Hello, I’ve been using an older PHP script (version 5.2) to connect to my a MYSQL Database. Recently I switched to a more updated PHP version. I believe Ive updated the PHP code properly, but for some reason, it will wont connect to MYSQL Database correctly. I was hoping for some help.
Here is the current PHP script
<?php
include "connect.php";
$datalink = mysqli_connect($hostname,$username, $password);
if (!$datalink) {
die('Unable to connect to database! Please try again later.' . mysqli_error());
}
mysqli_select_db;
****$sql = 'SELECT DISTINCT `formation` FROM `football_play_finder` ORDER BY `formation` ASC'; ****
****/* ORDER BY playbook ASC */****
$query = $sql;
$result = mysqli_query or mysqli_error;
$header = mysqli_num_fields;
//echo '<p class="Heading"><strong> Play Finder</strong></p>';
//------------------------------------------------------------
// List Box
echo '<form id="form1" name="form1" method="post" action="formation.php">
<label><p class="Text"><strong>Choose Formation:
</strong></p></lable>
<select name="formation" id="formation">';
while ($row = mysqli_fetch_row($result))
{
for ($i=0; $i<$header; $i++)
{
if (!isset($row[$i])) //test for null value
{
echo "NULL";
}
else
{echo '<option value="' . $row[$i] . '">' . $row[$i].'</option>';}
}
}
echo '
</select>
</label>
</p>
<p>
<input type="submit" name="Select" id="Select" value="Submit" />
</p>
</form>';
mysqli_close($datalink);
?>
I think this part of the code may be the problem
mysqli_select_db;
**$sql = 'SELECT DISTINCT `formation` FROM `football_play_finder` ORDER BY `formation` ASC'; **
**/* ORDER BY playbook ASC */**
football_play_finder is the data base I am trying to connect to
If someone could help me, it would be appreciated.
The database connection WORKS (otherwise you’d be seeing the “Unable to connect” error message).
The problem you’re having is that the query is not returning data.
… where in that do you tell the mysqli_query function what SQL to send?
I think you meant to do something like…
$result = mysqli_query($sql) or die(mysqli_error());
m_hutley, that is correct, it does connect. I don’t get any errors. Matter of fact I actually purposely put in the wrong password for database to see if I I would get an error and of course I did. Once i put the correct password back in, it connected. so that is not the issue. I tried to the code you posted, and I got this error:
Warning: mysqli_query() expects at least 2 parameters, 1 given in /home3/maddenguides/public_html/playfinder/index.php on line 58
Warning: mysqli_error() expects exactly 1 parameter, 0 given in /home3/maddenguides/public_html/playfinder/index.php on line 58
No… ADD it to the connect command. The database server can hold many databases. When you connect to it, the server needs to know which database you’re trying to read/write to.