r/PHPhelp • • Sep 12 '21

Solved Error Running a Simple SQL Query with PHP

Hello,

I am trying to run a very simple query, but I can't quite seem to get it to work. I have tried several variations of StackOverflow answers, but nothing displays beyond my simple "Connected Successfully" echo statement. I am trying to display the query on a webpage at the moment, but eventually I want to display it on a leaderboard through Unity.

Does anyone know what's wrong? Thank you to everyone in advance

$database = 'database';

$username = 'username';

$hostname = 'hostname';

$password = 'password';

// checking connectivity

$con = mysqli_connect($hostname,$username,$password,$database);

if (mysqli_connect_errno()) {

echo "Failed to connect to MySQL: " . mysqli_connect_error();

exit();

}

else {

echo " Connected Successfully ";

}

// running query

$query = "SELECT * FROM TestScores";

if ($result = $mysqli->query($query)) {

while ($row = $result->fetch_assoc()) {

$field1name = $row["col1"];

$field2name = $row["col2"];

echo $field1name;

}

}

Here is another variation I tried to no success:

$sql="Select * from TestScores";

$result=mysql_query($sql) or die(mysql_error());

while($myrow=MySQL_fetch_array($result,MYSQL_ASSOC))

{

extract($myrow);

echo "<td align='left' bgcolor='black'>".$myrow[0]."</td>";

echo "<td align='left' bgcolor='black'>".$myrow[1]."</td>";

}

Thank you

7 Upvotes

9 comments sorted by

3

u/Blackhaze84 Sep 12 '21

Double check that the keys of this assoc array are correct. If not, you are echoing keys that don't exist.

2

u/TheGreenSquier Sep 12 '21

Ahhh, this was it. The column names were "Names" and "Scores", not "col1" and "col2". Thank you!!!!!!!

2

u/Blackhaze84 Sep 12 '21 edited Sep 12 '21

Glad to help you! btw, did yoku enable error and warnings messages? By default they are disabled but are very helpful. Check the php.ini file configuration.

1

u/TheGreenSquier Sep 12 '21

No, I haven't done that. Do you know where I can find it? My PHP files are just on a hosting platform called Hostinger, I don't see a php.ini file

1

u/zaval Sep 12 '21

So you're doing query on the variable $mysqli. Have a look and see if that's right for you. Did you mean to use $con?

2

u/TheGreenSquier Sep 12 '21

Ahh this was definitely one mistake. With your comment and the other comment, I got it to work. Thank you so much!!

2

u/zaval Sep 12 '21

Awesome!!

2

u/TheGreenSquier Sep 12 '21 edited Sep 12 '21

As a follow up, what function would be help me separate the data? It currently displays as:

Noa5Amy750Brett500Richard250Richard250ChuckNorris1000

Thanks again

*edit:*

Got them kind of separated using:

$result = mysqli_query($mysqli,$query);

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

printf ("%s %s \n", $row["Name"], $row["Score"]);}

Now to get this into Unity...

1

u/Nerwesta Sep 13 '21

You need to separate the datas on your presentation layer, basically what you're doing here was to drop the raw datas without proper html and css on your webpage.