Help with PHP and MYSQL Data Base

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.

Thanks

So to be clear;

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());
1 Like

It’s a while since I used mysqli so I don’t remember the syntax too well (I use PDO exclusively now) but the syntax does not look right to me.

Here you pass the query string from one variable to another, I don’t see the point of that. You then do nothing with either variable.

The mysqli_query function requires parameters to work, the connection and the query.

Do you have error reporting on? That should tell you where you are going wrong.

1 Like

Sam, thanks for response, I did not get an error or anything, just no data being pulled. I can send a you a link if that helps.

Thanks

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

Thanks

Woops. forgot i’m doing procedural and not object.

$result = mysqli_query($datalink,$sql) or die(mysqli_error($datalink));

Weird, I added that code and I get.

No database selected

I know I got the right info in.

Just not sure why it wont connect.

your connect command doesnt specify which database you want to interact with.

<?php

//Connect To Database

$hostname="localhost";
$username="maddengu_football";
$password="mypassword";
$dbname="football_play_finder";

?>

That is my connect.php file I made

Anything wrong there?

You define four values there.
$datalink = mysqli_connect($hostname,$username, $password);
You only use three here.

So remove this one

$dbname=“football_play_finder”;

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.

$datalink = mysqli_connect($hostname,$username, $password, $dbname);

1 Like

I am confused on what connect command I need to add to interact with the database.

replace this line…

$datalink = mysqli_connect($hostname,$username, $password, $dbname);
with this one.

I did that, but now I get this

Table ‘football_play_finder.football_play_finder’ doesn’t exist

The table name is called playfinder

I know there is something is this part of the code that is preventing me from connecting

$sql = ‘SELECT DISTINCT playfinder FROM football_play_finder ORDER BY formation ASC’;

I am in phpmyadmin control panel

here is what it shows

Database: football_play_finder »Table: playfinder

So should the query be:-

SELECT DISTINCT playfinder FROM playfinder ORDER BY formation ASC

A typical query is like:-

SELECT table_fields FROM table_name ...

You seem to have:-

SELECT table_name FROM database_name ...

Parse error: syntax error, unexpected ‘DISTINCT’ (T_STRING) in /home3/maddenguides/public_html/playfinder/index.php on line 53

Getting close

I fixed the syntax error

unexpected ‘DISTINCT’ (T_STRING)

Now I am getting this

Unknown column ‘playfinder’ in ‘field list’

Now sure what that means

This:-

After SELECT you define the columns that you want to select.