I know the basics of HTML, and nothing about PHP. I've found a ton of tutorials, but I couldn't get a single one to work. I have a mySQL database working and apache running. How can I display an entire table on my page? I do not know the column names ahead of time.
Edit: Added and adjusted the code. My index page shows this: connect_errno) { echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; } //$sql = "SHOW TABLES"; $sql = "select * from cpu"; //edit your table name here $res = $mysqli->query($sql); while ($row = $res->fetch_assoc()) { print_r($row); } ?>
This is my entire index.html source:
<?php
$mysqli = new mysqli("192.168.1.2", "webuser", "*****", "OCN");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
//$sql = "SHOW TABLES";
$sql = "select * from cpu"; //edit your table name here
$res = $mysqli->query($sql);
while ($row = $res->fetch_assoc()) {
print_r($row);
}
?>
Once I changed my file to .php, the error message was more clear. It reminded me that I didn't have webuser at 192.168.1.2. I created the user again and gave the correct permissions. The function appears to work, but I'll have more time in a few hours to look at it.
The real problem here is that you see connect_errno) { echo "Failed to... on your page, instead of a line beginning with Failed to... only.
Because your browser thinks that the code from <?php to $mysqli-> is an HTML tag (because of the opening and closing <>, I think your file is a .htm(l) file rather than a .php file.
Just change the file extension and if PHP is installed correctly it should work.
<?php
$mysqli = new mysqli("localhost", "root", "", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
//$sql = "SHOW TABLES";
$sql = "select * from tbl_comment"; //edit your table name here
$res = $mysqli->query($sql);
while ($row = $res->fetch_assoc()) {
print_r($row);
}
?>
From what I understood. You want to display values but you don't know column names.
If that's so, try this.
$mysqli = new mysqli("192.168.1.2", "webuser", "*****", "OCN");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
//$sql = "SHOW TABLES";
$sql = "select * from cpu"; //edit your table name here
$res = $mysqli->query($sql);
while ($row = $res->fetch_assoc()) {
foreach($row as $ind => $val)
{
echo "Column name: $ind, Column Value: $val<br />";
}
echo "<hr />";
}
?>
Related
Php results that excluded null database entries have now stopped working.
I produced the following test code and it delivers nulls in the result. I am baffled and haven't a clue how to solve. Please help a 75 year old and my first time on an internet question site
<body>
<?PHP
include "dbcfg.php";
$link = mysqli_connect($mysqlserver, $mysqlusername, $mysqlpassword, $dbname) or
die("Error connecting to mysqli server: " . \mysqli_error());
if (!$link) {
die("Connection failed: " . mysqli_connect_error());
}
$attractquery = "select * from eatingout where town = 'Newcastle'";
$result = \mysqli_query($link, $attractquery) or die
("Query to get data from table failed: " . \mysqli_error());
while ($db_row = mysqli_fetch_array($result)) { // iterate through all selected
/*if($db_row['image'] != null){*/
/*if (!empty($db_row['image']));{*/
if(isset($db_row['image']));{
echo $db_row['image'] ." ". $db_row['EOID']."<br/>" ;
}
}
?>
</body>
Results from the above include numerous Nulls followed by EOID numbers from any of the three options. What have I done?
You can add NULL test in query:
$attractquery = "select * from eatingout where town = 'Newcastle' AND image is NOT NULL";
Might be a string conversion problem?
while ($db_row = mysqli_fetch_array($result)) {
if(isset($db_row['image']) && $db_row['image'] != NULL && strtolower($db_row['image']) != 'null' ){
echo $db_row['image'] ." ". $db_row['EOID']."<br/>" ;
}
}
Please check that your columns containt a NULL value and not a string that says "NULL". You should enable for column image to have NULL value by ticking the checkbox for NULL
<body>
<?PHP
include "dbcfg.php";
$link = mysqli_connect($mysqlserver, $mysqlusername, $mysqlpassword, $dbname) or
die("Error connecting to mysqli server: " . mysqli_error());
if (!$link) {
die("Connection failed: " . mysqli_connect_error());
}
$attractquery = "select * from eatingout where town = 'Newcastle' and ifnull(image,'')=''";
$result = mysqli_query($link, $attractquery) or die
("Query to get data from table failed: " . mysqli_error());
while ($db_row = mysqli_fetch_array($result)) { // iterate through all selected
/*if($db_row['image'] != null){*/
/*if (!empty($db_row['image']));{*/
if(!is_null($db_row['image'])){
echo $db_row['image'] ." ". $db_row['EOID']."<br/>" ;
}
}
?>
</body>
Kindly remove '\' before mysqli_query and mysqli_error.
You should use !is_null($db_row['image']) instead of isset bcoz $db_row['image'] will always be set.
Secondly you can check the null value in query it self, as mentioned in other answer.
third there is a colan after if clause ie
if(isset($db_row['image']));{
echo $db_row['image'] ." ". $db_row['EOID']."<br/>" ;
}
I am starting to use mysqli and I got it working on my virtual server but can't get it working on my real server. The databases are the same. I have tried both store and get_result. Any idea what am I doing wrong?
Everything I try to echo, print or var_dump does not show up IF Place after execute but the command will be executed. It worked well on UPDATE and INSERT.
<?php
$mysqli = new mysqli("127.0.0.1", "user", "pass", "db", 3306);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n";
$sam = 1;
$stmt = $mysqli->prepare("SELECT released FROM svers");
$stmt->execute();
echo "#";
var_dump($sam);
$res = $stmt->get_result();
var_dump($res);
$row = $res->fetch_assoc();
var_dump($row);
$mysqli->close();
The host_info is displayed as "127.0.0.1 via TCP/IP" followed By the # I echoed inside the if() but can't get anything from my database. Even the other # between the $row[ ] info are not showing. Here is my code:
<?php
$mysqli = new mysqli("127.0.0.1", "user", "password", "database", 3306);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n";
$stmt = $mysqli->prepare("SELECT * FROM svers ORDER BY released DESC LIMIT 1");
if ($result = $stmt->execute()){
echo "#";
$result = $stmt->get_result();
$row = $result->fetch_array(MYSQLI_BOTH);
echo $row['version'] . "#" . $row['released'] . "#" . $row['note'];
$stmt->free_result();
}else {
echo "error";
}
$mysqli->close();
?>
Ok problem solved. My real server didn't support mysqlnd so can't use get_result(). Thanks for the help guys!
You are saying got it working on my virtual server but can't get it working on my real server.
Then check svers table on real server contains any data.
For the code below added, i am not getting any result printed.
$con = #mysqli_connect("localhost","root","","temp");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query="SELECT * FROM `login`";
echo $query;
$result=#mysqli_query($query) or die(mysql_error());
while($row=mysqli_fetch_array($result))
{
echo $row["username"];
}
Try the below code it will work
//conection:
$con = mysqli_connect("localhost","root","","temp") or die("Error " . mysqli_error($con));
//consultation:
$query = "SELECT * FROM login" or die("Error in the consult.." . mysqli_error($con));
//execute the query.
$result = $con->query($query);
//display information:
while($row = mysqli_fetch_array($result)) {
echo $row["username"] . "<br>";
}
Use this code as it is.
$con=mysqli_connect("localhost","root","","temp");
$result = mysqli_query($con,"SELECT * FROM login");
while($row = mysqli_fetch_array($result))
{
echo $row["username"];
}
// use this code and plz check your db name
$host='localhost';
$user='root';
$pass='';
$db_name='temp';
$con=mysqli_connect($host,$user,$pass,$db_name);
if($con)
{
echo "db connect succecssfully";
}
$slt="select * from login";
$query=mysqli_query($slt,$con);
while($row=mysqli_fetch_array($query))
{
echo $row["username"];
}
<?php
$con=mysqli_connect("localhost","root","","temp");
// Here localhost is host name, root is username, password is empty and temp is database name.
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform queries
$result = mysqli_query($con,"SELECT * FROM login");
while($row = mysqli_fetch_array($result)) {
echo $row["username"] . "<br>";
}
mysqli_close($con);
?>
Use this. it may solve your problem.
//connection
$con = mysqli_connect("localhost","root","","my_db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
I've trying to display values from mysql but it return any empty page. The connection is fine but it does not fetch the data from mysql. I tried all the answers from the similar questions asked. But nothing helped. Can somebody please help me? This is the code
$con= mysql_connect($host, $username, $pwd);
if(!$con)
die("not connected". mysql_errno());
echo(Connected);
mysql_select_db("info",$con);
$query="select * from people";
$result= mysql_query($query,$con) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo $row['id']. " - ". $row['people_name'];
echo "<br />";
}
Try to check if your db user,password are correct! I test the code above :
<?php $con=mysqli_connect("localhost","root","","test"); // Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM people");
while($row = mysqli_fetch_array($result)) {
echo $row['id'] . " -- " . $row['people_name']; echo "<br>";
}
?>
and give me the result without error: 10 -- JOHN 11 -- PRADEEP
I just change mysql_connect to mysqli_connect add in $con= mysql_connect($host, $username, $pwd); a dbname. and $con become $con= mysqli_connect($host, $username, $pwd,$dbname); I use mysqli_query instead of mysql_query. Here is a stackQuestion for the mysql vs mysqli in php which can explain you the difference.
Try this
<?php
$con= mysql_connect('hostname', 'username', 'password');
if(!$con)
die("not connected". mysql_errno());
echo("Connected");
mysql_select_db("test",$con);
$query="select * from tabale_name";
$result= mysql_query($query,$con) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo $row['id']. " - ". $row['name'];
echo "<br />";
}
?>
check this
<?php
$con=mysqli_connect("hostname","username","password","info");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM people");
while($row = mysqli_fetch_array($result))
{
echo $row['id'] . " " . $row['people_name'];
echo "<br>";
}
?>
OR
<?php
$con=mysqli_connect("hostname","username","password");
// Check connection
if ($con)
{
echo "connected to db";
}
else
{
echo "not connected to db";
}
$db_selected = mysql_select_db("info", $con);
if (!$db_selected)
{
die ("Can\'t use info: " . mysql_error());
}
$result = mysqli_query("SELECT * FROM people");
while($row = mysqli_fetch_array($result))
{
echo $row['id'] . " " . $row['people_name'];
echo "<br>";
}
?>
I am trying use mysqli functions but I get this error:
Fatal error: Call to undefined method mysqli::num_rows() in C:\AppServ\www\edu\files\header.php on line 19
I tried to go to php.ini and remove ; from extension=php_mysqli.dll
I found it already removed
I tried to restart appachi;
the connection file:
// db username
define("USERNAME","root");
// db password
define("PASSWORD","root");
// db servername
define("SERVERNAME","localhost");
// db name
define("NAME","edu");
//connect to db
$mysqli = new mysqli(SERVERNAME,USERNAME,PASSWORD,NAME);
if ($mysqli->connect_errno) {
echo $cannot_connect;
}
//select db encoding
$mysqli->set_charset('utf8');
calling function in a file:
$sql = $mysqli->query("SELECT VALUE FROM SITE_CONFIG WHERE CONF='KEYWORDS'");
if (!$sql) {
echo "Failed to run query: (" . $mysqli->errno . ") " . $mysqli->error;
}
if($mysqli->num_rows($sql) > 0){
while($rs = $sql->fetch_assoc()){
$keyw = $rs['VALUE'];
}
}else{
$keyw = $no_data;
}
note : I included connection.php
Try:
$sql = $mysqli->query("SELECT VALUE FROM SITE_CONFIG WHERE CONF='KEYWORDS'");
if (!$sql) {
echo "Failed to run query: (" . $mysqli->errno . ") " . $mysqli->error;
} else {
if($sql->num_rows > 0){// here must be $sql , not $mysqli
while($rs = $sql->fetch_assoc()){
echo $rs['VALUE'];
}
} else{
echo "No data";
}
}