Jump to content

'SELECT * FROM `tableA` WHERE 1'


ScrewLooseSalad

Recommended Posts

I want to use PHP to print the results of

 

'SELECT * FROM `tableA` WHERE 1'

 

Ithe printout in MySQL in terminal is exactly what I need displayed on the website, but I'm ony getting into interfacing MySQL and PHP and I don't know how to go about it, can anyone help? What I've tried doesn't seem to work, I can't get anything to output.

 

Can anyone point me in the right direction?

thanks

Link to comment
Share on other sites

<?php
$db = new mysqli('localhost', 'stockuser', 'password', 'stockdatabase');
if (mysqli_connect_errno()) {
echo 'Error: Could not connect to database. Please try again later.';
echo mysqli_connect_error();
echo 'end printout';
exit;
}

// perform query
$query = "SELECT * FROM '%stocktable%' WHERE 1";
echo '<br>query set up';

$result = $db->query($query);
echo ' query entered,'

// get number of returned rows
$num_results = $result->numRows();
echo '<br>calculated rows, about to readout results<br>';

// display each returned row
for ($i=0; $i <$num_results; $i++) {
$row = $result->fetch_assoc();
echo "<p><strong>".($i+1).". PartID: ";
echo htmlspecialchars(stripslashes($row['PartID']));
echo "</strong><br />Part_Name: ";
echo stripslashes($row['Part_Name']);
echo "<br />Stock: ";
echo stripslashes($row['Stock']);
echo "<br />Supplier: ";
echo stripslashes($row['Supplier']);
echo "</p>";
}
}

echo '<br><br><br><br>script completed';
?>

 

I've identified the line "$result = $db->query($query);" to be the one preventing the script from running but I can't figure out why. I've got extra echos in there that helped me figure that out, with this line uncommented none of them show up...

Edited by ScrewLooseSalad
Link to comment
Share on other sites

Make sure you have error reporting turned on ,then capture MySQL errors. A blank page means you have a fatal PHP error, which you should be able to see by enabling error_reporting set to E_ALL.

 

If this is your actual code, the problem is the line after the query(). You're missing something vital.

Link to comment
Share on other sites

Make sure you have error reporting turned on ,then capture MySQL errors. A blank page means you have a fatal PHP error, which you should be able to see by enabling error_reporting set to E_ALL.

 

If this is your actual code, the problem is the line after the query(). You're missing something vital.

 

if its a fatal error, would 'error_reporting' still work? I'll try to figure out whats missing now...

 

 

Your query is invalid:

SELECT * FROM '%stocktable%' WHERE 1

 

What exactly are you trying to do?

 

It works in the terminal, I just want to print out the contents of the Table, as a lists the company stock

Link to comment
Share on other sites

You really should rename your table so it's just stocktable. Or even better: stock.

 

And WHERE 1 is superfluous.

 

Is that not what I called it? I thought you needed the % or it misinterpreted the ' symbol or something, one of the things I've tried to get that line to work, as it wouldn't when I first wrote it without the %

Link to comment
Share on other sites

A query should be:

SELECT fields FROM table.

 

You use quotes around strings, and % is a wildcard for searching.

 

However if that exact query works in command line, your table is named literally '%stocktable%'

 

Do SHOW TABLES; and see what it says.

Edited by Jessica
Link to comment
Share on other sites

A query should be:

SELECT fields FROM table.

 

You use quotes around strings, and % is a wildcard for searching.

 

However if that exact query works in command line, your table is named literally '%stocktable%'

 

Do SHOW TABLES; and see what it says.

 

'%stocktable%' doesn't work in the command line, but 'stocktable' does; I misunderstood the %, I thought the PHP preprocessor wanted them as I saw it being used that way

Link to comment
Share on other sites

Here's a generic function for displaying query results.

 

function query2table($db, $sql)
{
   $output = "<table border='1'>\n";
   // Query the database
   $result = $db->query($sql);
   // check for errors
   if (!$result) return ("$db->error <pre>$sql</pre>");

   // get the first row and display headings
   $row = $result->fetch_assoc();
   $output .= "<tr><th>" . join('</th><th>', array_keys($row)) . "</th></tr>\n";

   // display the data
   do {
   $output .= "<tr><td>" . join('</td><td>', $row) . "</td></tr>\n";
   } while ($row = $result->fetch_assoc());
   $output .= "</table>\n";
   return $output;
}

 

Just pass it your mysqli object and query

 

$db = new mysqli(HOST, USERNAME, PASSWORD, DATABASE);

$sql = "SELECT * FROM stocktable";

echo query2table($db, $sql);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.