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

1

u/wh33t Nov 01 '17

pHp coffee to call SQL statements from a database

What? What are you trying to do here?

1

u/youngjeeez Nov 01 '17

sorry that was a typo, what i meant was php code to return an entire sql statement from a mysql database

1

u/wh33t Nov 01 '17

Alright, what is the statement you are trying to run? If you don't know that, what information from which database/table are you trying to retrieve?

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

Thanks so much, really appreciate it