{"id":26871274,"url":"https://github.com/codeadamca/php-sql-mysql","last_synced_at":"2026-02-19T16:32:18.329Z","repository":{"id":115328460,"uuid":"298918469","full_name":"codeadamca/php-sql-mysql","owner":"codeadamca","description":"An introduction to using PHP with MySQL.","archived":false,"fork":false,"pushed_at":"2025-01-26T21:39:33.000Z","size":21,"stargazers_count":0,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-31T07:18:53.946Z","etag":null,"topics":["learning-code","mysql","php","sql"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/codeadamca.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-09-26T23:47:06.000Z","updated_at":"2025-01-26T21:39:37.000Z","dependencies_parsed_at":null,"dependency_job_id":"878505a5-18be-48ff-89fb-16f8d3660cb9","html_url":"https://github.com/codeadamca/php-sql-mysql","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeadamca%2Fphp-sql-mysql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeadamca%2Fphp-sql-mysql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeadamca%2Fphp-sql-mysql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeadamca%2Fphp-sql-mysql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codeadamca","download_url":"https://codeload.github.com/codeadamca/php-sql-mysql/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252826495,"owners_count":21810121,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["learning-code","mysql","php","sql"],"created_at":"2025-03-31T07:18:57.538Z","updated_at":"2025-10-20T00:24:58.737Z","avatar_url":"https://github.com/codeadamca.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# A Basic Introduction to PHP and MySQL\n\nThis tutorial will review how to use SQL and PHP to display data from a MySQL database in an HTML webpage. \n\n## The End Goal\n\nThe `links.sql` file in the repository includes a list of links that can be imported into your MySQL database. Once those have been imported we will use PHP and SQL to display the data from the MySQL database in an HTML webpage. \n\nThere are multiple methods of retrieving data from a MySQL database using PHP. For simplicity sake the example below will use a series of `mysqli` PHP functions. \n\n## Steps\n\n1. Open up phpMyAdmin.\n\n    If you're using a local server phpMyAdmin can usually be accessed by starting your server and then clicking on the phpMyAdmin link. If you're using a hosting account there will be a link to phpMyAdmin in your control panel. \n    \n    Once you have phpMyAdmin open, click on the import tab and select the `links.sql` file from this repository. This will create a table called `links` and populate it with some sample data. \n\n2. Create a new file and name it `links-list.php`. In that file place the standard HTML tags. \n\n3. Above the `doctype` tag add some PHP to connect to your database:\n    \n    ```php\n    \u003c?php\n    \n    $connect = mysqli_connect('localhost', 'root', 'root', 'sandbox');\n    \n    if (!$connect) \n    {\n        echo 'Error Code: ' . mysqli_connect_errno() . '\u003cbr\u003e';\n        echo 'Error Message: ' . mysqli_connect_error() . '\u003cbr\u003e';\n        exit;\n    }\n    \n    ?\u003e\n    ```\n\n    The first line of PHP will initiatie a connection to your MySQL server. The `mysqli_connect` function requires a host, username, password, and database name. \n    \n    If you are using a local PHP server link MAMP or WAMP your host is `localhost` and your username and password are likely both `root`. This may vary depending on how you set up your local host. The database name will be whataver you named your database. If you don't have one go ahead an create one. \n    \n    If you are using a hosting account, your MySQL user, password, and database will need to be created in your hosting control panel. There is likely help in your control panel on what to use for your host. \n    \n    The second part of the above code will display an error message if the MySQL connection fails. \n    \n    \u003e [More information on PHP and `mysqli_connect()`](https://www.php.net/manual/en/function.mysqli-connect.php)\n    \n\u003e [!Note]\n\u003e It would be a good idea to test at this point, nothing else below will work if your MySQL connection isn't working. \n\n4. In the body section of your document add the following PHP:\n\n    ```php\n    \u003c?php\n    \n    $query = 'SELECT id,name,url,image,description\n      FROM links\n      ORDER BY name';\n    $result = mysqli_query($connect, $query);\n    \n    if (!$result)\n    {\n      echo 'Error Message: ' . mysqli_error($connect) . '\u003cbr\u003e';\n      exit;\n    }\n    \n    echo '\u003cp\u003eThe query found ' . mysqli_num_rows($result) . ' rows:\u003c/p\u003e';\n    \n    ?\u003e\n    ```\n    \n    The first block of PHP will create and execute an SQL statement. If this query is successful then inside the `$result` variable will be a list of links. The next block of code will display an error message if the query failed. The final line of PHP will inform us as to how many records the query retrieved.\n\n    \u003e [More information on PHP and `mysqli_query()`](https://www.php.net/manual/en/mysqli.query.php)\n\n4. After the query has executed successfully, add this PHP:\n\n    ```php\n    \u003c?php\n    \n    while ($record = mysqli_fetch_assoc($result))\n    {\n    \n      echo '\u003chr\u003e';\n      echo '\u003ch2\u003e' . $record['name'] . '\u003c/h2\u003e';\n      echo '\u003cp\u003e' . $record['url'] . '\u003c/p\u003e';\n    \n      echo '\u003cpre\u003e';\n      print_r($record);\n      echo '\u003c/pre\u003e';\n    \n    }\n    \n    ?\u003e\n    ```\n    \n    The above code will loop through each record and display the link. From here the link information will need to be formatted using HTML just like the [php-for-while](https://github.com/codeadamca/php-for-while) and [php-if-switch](https://github.com/codeadamca/php-if-switch) examples.\n\n    \u003e [More information on PHP and `mysqli_fetch_assoc()`](https://www.php.net/manual/en/mysqli-result.fetch-assoc.php)\n\n\u003e Full tutorial URL:  \n\u003e https://codeadam.ca/learning/php-sql-mysql.html\n\n***\n\n## Repo Resources\n\n* [Visual Studio Code](https://code.visualstudio.com/) or [Brackets](http://brackets.io/) (or any code editor)\n* [Filezilla](https://filezilla-project.org/) (or any FTP program)\n\n\u003cbr\u003e\n\u003ca href=\"https://codeadam.ca\"\u003e\n\u003cimg src=\"https://cdn.codeadam.ca/images@1.0.0/codeadam-logo-coloured-horizontal.png\" width=\"200\"\u003e\n\u003c/a\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodeadamca%2Fphp-sql-mysql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodeadamca%2Fphp-sql-mysql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodeadamca%2Fphp-sql-mysql/lists"}