r/PHPhelp • • Nov 01 '17

Php code to call sql statements

Could someone help me with the pHp coffee to call SQL statements from a database

Thanks in advance

9 Upvotes

20 comments sorted by

View all comments

Show parent comments

1

u/youngjeeez Nov 01 '17

<?php include '../Connection.php'; if ($_SERVER["REQUEST_METHOD"] == "POST") { $results = mysqli_query($con, $_POST['Search']); if($results){ echo $results; header("Location:../CustomerRecords.php"); }else{ echo "No Results, Please try another query"; } }

1

u/wh33t Nov 01 '17

$results in this case is not data, it's a result ID, you need to actually fetch the data out of the database.

If you want one row of data

$results = mysqli_query($con, $_POST['Search']);
$single_row_of_data = mysqli_fetch_assoc($results);

If you want multiple

$results = mysqli_query($con, $_POST['Search']);
while($row = mysqli_fetch_assoc($results))
{
  echo $row;
}

Also, you should be sanitizing that $_POST['search'] data so that you can't be easily SQL injected. If it's just a personal project running on your own machine I wouldn't worry about it, but if people from the raw internet will be hitting this thing you got some security work to do.

1

u/youngjeeez Nov 01 '17

<?php include '../Connection.php'; if ($_SERVER["REQUEST_METHOD"] == "POST") { $results = mysqli_query($con, $_POST['Search']); if($results)while($row = mysqli_fetch_assoc($results)) { echo $row; }else{ echo "No Results, Please try another query"; } } that returns an error mysqli_query() empty

1

u/wh33t Nov 01 '17

Ok, what error is it?

What is it you are trying to do here exactly?

1

u/youngjeeez Nov 01 '17

The user needs to type SELECT * FROM ... Then this code now should run it

This is the error :

Warning: mysqli_query(): Empty query Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given

3

u/RandyHoward Nov 01 '17

The user needs to type SELECT * FROM

Oh lordy, you should not let your users enter SQL queries. You are just setting yourself up for trouble doing that. Tell us what your end goal is, because you seem to be approaching the entire problem the wrong way.

1

u/wh33t Nov 01 '17

Do you have phpmyadmin or some other mysql gui you can use to insure the select statement is actually returning results? Are you expecting it to return results?

-2

u/colshrapnel Nov 01 '17

You're an illiterate idiot. I wonder whether anyone would bother to ban you from this sub, but it will be a great riddance.