PHP mysqli not woking [duplicate] - php

This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 6 years ago.
So I'm new to mysqli. All of the examples I find online seem to be the old (procedural) way of doing things. Can someone tell me why my code isn't working below? My db is 'templatedb'. My Table is 'template'. I have one entry in my table, but I'm receiving no output with my echo. I'm not getting any errors with my code.
<div id="templateSelector">
<?php
$hostname = "localhost";
$username = "root";
$password = "";
$db = "templatedb";
//connect
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$database = mysqli_connect($hostname, $username, $password, $db);
if(!$database){
die("Could not connect to the database");
}
if ($database->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
} else {
$sql = "SELECT * FROM template";
if (!$result = $database->query($sql)) {
die('There was an error running the query [' . $db->error . ']');
} else {
echo "<label>Select Template</label>";
echo "<select name='templates'>";
while ($row = $result->fetch_assoc()) {
echo "hello";
echo $row['template_name'];
// echo "<option value='" . $row['template'] . "'>" . $row['template'] . "</option>";
}
echo "</select>";
}
}
?>

Try doing the following, worked for me
<div id="templateSelector">
<?php
$hostname = "localhost";
$username = "root";
$password = "";
$db = "templatedb";
$mysqli = mysqli_connect($hostname, $username, $password, $db);
if($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
} else {
$sql = "SELECT * FROM template";
$result = mysqli_query($mysqli, $sql);
if(!$result = $mysqli->query($sql)) {
die('There was an error running the query [' . $db->error . ']');
} else {
echo "<label>Select Template</label>\n";
echo "<select name='templates'>\n";
while($row = $result->fetch_assoc()) {
echo "<option>id = " . $row['id'] . "</option>\n";
}
echo "</select>";
}
}
?>
</div>
Make sure your DATABASE is called templatedb and the table it is in is called template and there is a row called id. I know that sounds trivial, but spelling mistakes will break your code.

Related

Get information from database

When I click on artikle It needs to diretct me to page.php where I display whole article.
Problem is im not sure how to with $_GET superglobal var properly take information. I have to get ID with $_GET.
I already have included database in my index.php where I displayed several articles.
Im sending id like this:
echo '<a href="clanak.php? class="form-field-textual" id='.$row['id'].'">';
//article.php
<?php
$servername = "localhost:3306";
$user = "root";
$pass = "";
$dbo = "projekt";
// Create connection
$conn = new mysqli($servername, $user, $pass, $dbo);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM projekt";
if ($conn->query($sql)===TRUE) {
$specID=$conn->insert_id;
echo "id: " . $row["id"]. " - Name: " .
$row["kategorija"]. " " . $row["naslov"]. "<br>";
} else {
echo "0 results";
}
$conn->close();
?>
$row_id = $row['id'];
echo 'some text';
<?php
$servername = "localhost:3306";
$user = "root";
$pass = "";
$dbo = "projekt";
// Create connection
$conn = new mysqli($servername, $user, $pass, $dbo);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$id = isset($_GET['id']) ? (int)$_GET['id'] : '';
$sql = "SELECT * FROM projekt WHERE id = $id";
if ($conn->query($sql)===TRUE) {
$specID=$conn->insert_id;
echo "id: " . $row["id"]. " - Name: " .
$row["kategorija"]. " " . $row["naslov"]. "<br>";
} else {
echo "0 results";
}
$conn->close();

PHP MySQL queries return no results

My queries to MySQL via PHP are returning no results. First, I have tried connecting and doing a select on a known table and get no results. I then try to get a listing of the tables and again no results. When I look at the database via phpMyAdmin I can see the tables and their contents. Here is my code. Can anyone offer some help as to what I am doing wrong?
<?php
# /* $ php -f db-connect-test.php */
echo"preparing to connect";
$dbname = '#########';
$dbuser = '#########';
$dbpass = '#########';
$dbhost = 'localhost';
$connect = #mysqli_connect($dbhost, $dbuser, $dbpass, $dbname) or die("Unable to Connect to '$dbhost'");
echo"<html>";
echo"<title>test page</title>";
echo"<body>";
echo"<h2> test page</h2>";
/* check connection */
if ($conn->connect_error) {
die("Connection failed: " . mysqli_connect_error());
}
else{
echo"Successfully Connected <p>";
}
if(mysqli_ping($connection)){
echo "got it<p>";
}
$sql = "SELECT * FROM `announcements`";
$result = mysqli_query($dbname, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo 'date: ' . $row['date'] . '\tTitle: ' . $row['title'] . '\tBody: ' . $row['body'] .'<br />';
}
} else {
echo "0 results<p>";
$sql = "SHOW TABLES";
$result = mysqli_query($dbname, $sql);
if (!$result) {
echo "DB Error, could not list tables<p>";
echo 'MySQL Error: ' . mysqli_error();
}
else{
while ($row = mysqli_fetch_row($result)) {
echo "Table: {$row[0]}<p>";
}
}
}
$conn->close();
echo"</body>";
echo"</html>";
?>
Here is the result I am seeing:
preparing to connect
test page
Successfully Connected
0 results
DB Error, could not list tables
MySQL Error:
end of results
For some reason I am unable to get MySQL to return a error message.
When calling mysqli_query()
mysqli_query($dbname, $sql);
The first parameter is your database link not the name...
mysqli_query($connect, $sql);
Also - don't use # for your connection (or preferably anywhere) as this suppresses errors.
Update:
Also just noticed...
mysqli_ping($connection)
which should be...
mysqli_ping($connect)
You just have to Copy and Paste this code
You don't have to use $dbname
Have to use $connect
<?php
# /* $ php -f db-connect-test.php */
echo"preparing to connect";
$dbname = '#########';
$dbuser = '#########';
$dbpass = '#########';
$dbhost = 'localhost';
$connect = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname) or die("Unable to Connect to '$dbhost'");
echo"<html>";
echo"<title>test page</title>";
echo"<body>";
echo"<h2> test page</h2>";
/* check connection */
if ($conn->connect_error) {
die("Connection failed: " . mysqli_connect_error());
}
else{
echo"Successfully Connected <p>";
}
if(mysqli_ping($connection)){
echo "got it<p>";
}
$sql = "SELECT * FROM `announcements`";
$result = mysqli_query($connect, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo 'date: ' . $row['date'] . '\tTitle: ' . $row['title'] . '\tBody: ' . $row['body'] .'<br />';
}
} else {
echo "0 results<p>";
$sql = "SHOW TABLES";
$result = mysqli_query($connect, $sql);
if (!$result) {
echo "DB Error, could not list tables<p>";
echo 'MySQL Error: ' . mysqli_error();
}
else{
while ($row = mysqli_fetch_row($result)) {
echo "Table: {$row[0]}<p>";
}
}
}
$conn->close();
echo"</body>";
echo"</html>";
?>

Displaying results of SQL query as a table?

I am trying to output the results of an SQL query as a table on a page on my website. I have found a few solutions online but I can't get any of them to work properly. Right now I copied and pasted a bit of code to just output the first two columns but I can't figure out how to get every column in a table. I am new to PHP and web development in general so any help would be appreciated.
My PHP:
<?php
SESSION_START() ;
$servername = "localhost";
$username = "MY USERNAME";
$password = "MY PASSSWORD";
$dbname = "MY DATABASE NAME";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
//$_session['userid'] = $userlogged;
$sql = "SELECT * FROM `climbs` WHERE `userlogged` = '" . $_SESSION['userid'] . "'";
$result = mysqli_query($conn,$sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Name</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["climb-id"]. "</td><td>" . $row["climbname"]. " " . $row["cragname"]. "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
mysqli_close($conn);
?>
check with var_dump :
some like that:
$result = mysqli_query($conn,$sql);
var_dump($result);
if ($result->num_rows > 0) {
maybe the query it's wrong.

CONCAT function in MySQL using PHP Undefined Variable

I need help on how to do this correctly. I need to execute this command:
SELECT concat(branchname, -->, itemtype, '(, quantity, ')') from monitoring
order by itemtype;
the syntax works in MySQL console. However, im having trouble with implementing it on php. I always get
"Undefined index: branchname"
"Undefined index: itemtype"
"Undefined index: quantity"
using this code:
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dex_test";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT concat(branchname,itemtype,quantity) from monitoring order by itemtype";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo " " . $row["branchname"]. " " . $row["itemtype"]. " ".$row["quantity"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
The error says it's in this line
echo " " . $row["branchname"]. " " . $row["itemtype"]. " ".$row["quantity"]. "<br>";
Im confused because I basically ran the same code that worked that lets me see the itemtype in the table:
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dex_test";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT itemtype FROM monitoring";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "itemtype: " . $row["itemtype"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
Help anyone?
It seems your query needs update
"SELECT concat(branchname,itemtype,quantity) from monitoring order by itemtype";
It should be
"SELECT branchname,itemtype,quantity from monitoring order by itemtype";
I have posted this answer in reference of how you were calling your fields in while loop
echo " " . $row["branchname"]. " " . $row["itemtype"]. " ".$row["quantity"]. "<br>";
and if you need to show the concat value within one field than it should be something like
$sql = "SELECT concat(branchname,' ',itemtype,' ',quantity) as branch from monitoring order by itemtype";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo $row["branch"]."<br>";
}
} else {
echo "0 results";
}
Just define the alias for the concatenated columns. Use this -
SELECT concat(branchname,itemtype,quantity) as branchname from monitoring order by itemtype
Or if you want them seperately then -
SELECT branchname, itemtype, quantityfrom monitoring order by itemtype

PHP-mysql_fetch_array return nothing

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>";
}
?>

Categories