I'm trying to put the tables from MySQL database in a HTML page using PHP.
I'm beginner in PHP and I face a problem to the mysqli_query function.
This is my PHP code:
// connect to the db
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'testdb';
$connection = mysqli_connect($host, $user, $pass, $db) or die("cannot connect to db");
// show the tables
$result = mysqli_query($connection, 'SHOW TABLES') or die ('cannot show tables');
while($tableName = mysqli_fetch_row($result)) {
$table = $tableName[0];
echo '<h3>', $table, '</h3>';
$result2 = mysqli_query($table, 'SHOW COLUMNS FROM') or die("cannot show columns");
if(mysqli_num_rows($result2)) {
echo '<table cellpadding = "0" cellspacing = "0" class "db-table">';
echo '<tr><th>Field</th><th>Type</th><th>Null</th><th>Key</th><th>Default</th><th>Extra</th></tr>';
while($row2 = mysqli_fetch_row($result2)) {
echo '<tr>';
foreach ($row2 as $key=>$value) {
echo '<td>',$value, '</td>';
}
echo '</tr>';
}
echo '</table><br />';
}
}
Unfortunately I get this error:
Warning: mysqli_query() expects parameter 1 to be mysqli, string given in
C:\xampp\htdocs\test\showtables.php on line 26, cannot show columns.
I've also tried with the mysql_query function, but I faced another error, I then I changed it back to the mysqli_query function.
This code show you all of the tables and tables rows. I try it works
<?PHP
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'testdb';
$mysqli = new mysqli($host, $user, $pass, $db);
//show tables
$result = $mysqli->query("SHOW TABLES from testdb");
//print_r($result);
while($tableName = mysqli_fetch_row($result))
{
$table = $tableName[0];
echo '<h3>' ,$table, '</h3>';
$result2 = $mysqli->query("SHOW COLUMNS from ".$table.""); //$result2 = mysqli_query($table, 'SHOW COLUMNS FROM') or die("cannot show columns");
if(mysqli_num_rows($result2))
{
echo '<table cellpadding = "0" cellspacing = "0" class "db-table">';
echo '<tr><th>Field</th><th>Type</th><th>Null</th><th>Key</th><th>Default</th><th>Extra</th></tr>';
while($row2 = mysqli_fetch_row($result2))
{
echo '<tr>';
foreach ($row2 as $key=>$value)
{
echo '<td>',$value, '</td>';
}
echo '</tr>';
}
echo '</table><br />';
}
}
?>
Sample Output :
In the second call to the mysqli_query function, you're passing in the table name instead of the connection. Try something like this:
$result2 = mysqli_query($connection, "SHOW COLUMNS FROM $table") or die("cannot show columns");
http://php.net/manual/en/mysqli.query.php
Related
I am trying to make an If statement in PHP loop for each row in an Sql table. I think I'm almost there but there is a syntax error that I cant quite figure out. Heres what i've got so far:
$mysqli = new mysqli("localhost", "root", "usbw", "favourites");
// check connection
if ($mysqli->connect_errno) {
die("Connect failed: ".$mysqli->connect_error);
}
$query = "SELECT * FROM defaultTiles";
$result = $mysqli->query($query);
while($row = $result->fetch_array()){
echo "<?php if ($tile_name === '$row[name]'){
$tile_colour = '$row[colour]';
$img_url = '$row[img_url]';
$link_url = '$row[link_url]';
} ?>";
}
Replace
while($row = $result->fetch_array()){
echo "<?php if ($tile_name === '$row[name]'){
$tile_colour = '$row[colour]';
$img_url = '$row[img_url]';
$link_url = '$row[link_url]';
} ?>";
}
with
while($row = $result->fetch_array()){
if ($tile_name === $row['name']){
$tile_colour = $row['colour'];
$img_url = $row['img_url'];
$link_url = $row['link_url'];
}
}
as, when you put an echo in front of the if statement, it just prints the statement inside the echo.
I am connecting to server with help of php for an android application.
Name of Database in phpmyadmin is "student" , name of table is "data" and fields are "Name" and "EmpId"
This is what I coded n php and getting the error on the "$output" part as undefined variable
Here is the code:
<?php
$connection = connectionserver ();
function connectionserver (){
$con = mysql_connect("localhost", "root", "") or die ("connection not found");
if($con)
echo "Connection Created" ,"<br>";
$database = mysql_select_db ("student1", $con);
if($database) echo "Database Connected" , "<br>";
return $con;
}
$result = mysql_query("select * from data");
while ($row = mysql_fetch_assoc($result))
{
$output [] = $row;
}
print json_encode($output);
mysql_close($connection);
?>
declare $output as array before the while
$output = array();
$undefined_array[] = 'something' will not trigger an E_NOTICE error. However it is good practice to initialize the variable.
The error comes from the line with json_encode, most likely because your query didn't return any result, didnt get into the while loop, thus $output[] was never executed.
You may try this:
<?php
$connection = connectionserver ();
function connectionserver (){
$con = #mysql_connect("localhost","root","");
if(!$con) die("Can't connect!!");
$var2 = #mysql_select_db("student1",$con);
if(!$var2)
die("<br>"."can't select dataBase");
$result = mysql_query("select * from data");
while ($row = mysql_fetch_assoc($result))
{
$output[] = $row;
}
print json_encode($output);
mysql_close($con);
}
?>
Try this,
echo connectionserver();
function connectionserver (){
$con = mysql_connect("localhost", "root", "") or die ("connection not found");
$database = mysql_select_db ("student1", $con);
$result = mysql_query("select * from data") or die(mysql_error());
$output = array();
while ($row = mysql_fetch_assoc($result))
{
$output[] = $row;
}
mysql_close($con);
return json_encode($output);
}
I have two rows in my MySQL data that I would like to have code echoed only if the MySQL row data is equal to '1' (as opposed to '0'). Here's the code so far, which seems to have some severe errors:
$query = "SELECT 162, 164 FROM search WHERE title = $title";
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_row()) {
if ($row["162"] = 1) {
echo '<div id="162link">1.6.2</div>';
}
}
if ($row["164"] = 1) {
echo '<div id="162link">1.6.2</div>';
}
}
}
$result->close();
}
$mysqli->close();
As it says in the code above the two rows are "162" and "164" in the database.
Use:
if ($row["162"] == 1)
Instead of:
if ($row["162"] = 1)
and:
if ($row["164"] == 1)
I tried for you something like this if it gives you some idea:
$host = "localhost";
$user = "myusername";
$pass = "mypassword";
$database = "WorldEngine";
$mysqli = new mysqli($host, $user, $pass, $database);
$title = "My Good News";
$query = "SELECT `162`, `164` FROM search WHERE title = '$title';";
if ($result = $mysqli->query($query)) {
$i = 0;
while ($row = $result->fetch_row()) {
if ($row["162"] == 1) {
echo '<div id="162link' . $i . '">1.6.2</div>';
}
if ($row["164"] == 1) {
echo '<div id="164link' . $i . '">1.6.4</div>';
}
$i++;
}
$result->free();
}
$mysqli->close();
The index $i is appended to the div ID in order to produce unique DOM element ID's in the HTML document. I would also suggest you to change your numerical column names into alphabet-starting names like c162, c164, ...
Hope this will help you.
I have created a series of web pages one creates users into a sql database another page looks up all of these users then needs to show them in a drop down list for another database input.
On calling the php file and using echo to print out the array it works fine however when i put it within my html file the drop down menu simply displays "$username_array[] = "\"".$row['Username'].""\" once I believe its something to do with escaping quotes however cant figure it out any help would be greatly appreciated!!
This is the code held within the html file
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$db = 'fid';
echo "<label class=\"input\" for=\"investigator\" type=\"input\">Investigator:<select id=\"investigator\" name=\"investigator\">";
$conn = mysql_connect($dbhost,$dbuser,$dbpass);
if (!$conn)
die('Could not connect: ' . mysql_error());
mysql_select_db($db);
$username_array = array();
$sql = mysql_query("SELECT `Username` FROM `user`");
while ($row = mysql_fetch_array($sql)){
//echo $username_array[] = "\"".$row['Username']."\"";
echo "<option value='null'>"$username_array[] = "\"".$row['Username'].""\"</option>";
}
echo "</label>";
mysql_close($conn)
?>
I want the username array to display one user after the other within a drop down list however currently just echos out the option value line without any interpretation
UPDATE
I've now changed the code to this
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$db = 'fid';
$conn = mysql_connect($dbhost,$dbuser,$dbpass);
if (!$conn)
die('Could not connect: ' . mysql_error());
mysql_select_db($db);
echo '<select id="investigator" name="investigator">';
$resource = mysql_query("SELECT `Username` FROM `user`");
if($resource && mysql_num_rows($resource)) {
while ($row = mysql_fetch_assoc($resource)){
echo '<option value="'.$row['Username'].'">'.$row['Username'].'</option>';
}
}
echo '</select>';
mysql_close($conn)
?>
however the html outputs the script rather than a drop down list and the usernames
html output
'; $resource = mysql_query("SELECT Username FROM user"); if($resource && mysql_num_rows($resource)) { while ($row = mysql_fetch_assoc($resource)){ echo ''; } } echo ''; mysql_close($conn) ?>
any help would be appeciated I just really need to get this working as its been bugging me for days!
This will do what you need
echo '<select id="investigator" name="investigator">';
$resource = mysql_query("SELECT `Username` FROM `user`");
if($resource && mysql_num_rows($resource)) {
while ($row = mysql_fetch_assoc($resource)){
echo '<option value="'.$row['Username'].'">'.$row['Username'].'</option>';
}
}
echo '</select>';
BUT you should also move to mysqli or PDO as mysql_* is depreciated
I have changed $sql to $resource as it is not an SQL statement but a resource.
I'd change your code to something similar to this:
echo "<select>";
while ($row = mysql_fetch_array($sql)) {
echo "<option value=\"VALUE\">".$row['Username']."</option>";
}
echo "</select>";
Why does this only print the sites specific content under the first site, and doesn't do it for the other 2?
<?php
echo 'NPSIN Data will be here soon!';
// connect to DB
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to DB');
$dbname = 'npsin';
mysql_select_db($dbname);
// get number of sites
$query = 'select count(*) from sites';
$result = mysql_query($query) or die ('Query failed: ' . mysql_error());
$resultArray = mysql_fetch_array($result);
$numSites = $resultArray[0];
echo "<br><br>";
// get all sites
$query = 'select site_name from sites';
$result = mysql_query($query);
// get site content
$query2 = 'select content_name, site_id from content';
$result2 = mysql_query($query2);
// get site files
// print info
$count = 1;
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "Site $count: ";
echo "$row[0]";
echo "<br>";
$contentCount = 1;
while ($row2 = mysql_fetch_array($result2, MYSQL_NUM)) {
$id = $row2[1];
if ($id == ($count - 1)) {
echo "Content $contentCount: ";
echo "$row2[0]";
echo "<br>";
}
$contentCount++;
}
$count++;
}
?>
The problem is that you assume that once your finished looking for the row with the same id as the site row, that it'll reset the $result2 query to the beginning. This means that after you find your first row (unless you were to sort the two queries), that the second pass of the while loop wouldn't have any results left. You should consider caching the inner while loop first and then using an array lookup to get the value.
An even better solution would involve a join from sites to content which wouldn't require this complex matching process. Joins are a VERY important part of SQL, I highly suggest learning how to use them. http://dev.mysql.com/doc/refman/5.0/en/join.html