PHP won't insert in mysql in XAMPP in linux - php

I've tried many things. This is the simplest form I can try to see if all is working.
<?php
$mysql_host = "xxxx";
$mysql_username="xxxx";
$mysql_password="xxxxx";
$mysql_database="test2";
// Create connection
$conn = new mysqli($mysql_host, $mysql_username, $mysql_password, $mysql_database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} echo "Connected successfully";
$sql = "INSERT INTO `test_table` (`id`,`firstname`) VALUES ('3','james')";
echo "-----------";
echo "-----------";
$sql = "SELECT * FROM `test_table`";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
The table already contains 2 dummy inputs that I can insert from phpmyadmin.
When I try the above I get:
Connected successfully----------------------id: 1 - Name: john
id: 2 - Name: ghost
I can't see where I am going wrong.
Could it be that I need some kind of permissions change for this in linux?
That would be odd, I don't remember doing this before.

Related

My PHP code doesnt seem to want to connect to the database and/or display the data, can someone point out what's wrong?

So basically I am trying to simply just connect to my local mysql database with a football table to display on the site, but it doesn't seem to be able to connect. I feel like it's really obvious but I just can't see it.. I have checked the password is correct also, and it is.
<?php
$servername = "localhost";
$username = "root";
$password = "password"
$dbname = "import_excel_to_mysql";
/* Create connection */
$conn = new mysqli("localhost", "root", "password", "import_excel_to_mysql");
/* Check connection */
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error . mysql_error());
}
$sql = "SELECT position, team_name, played, wins, draws, losses, goals_scored, goals_against, goal_difference, points FROM football table";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>Pos</th><th>Team Name</th><th>P</th><th>W</th><th>D</th><th>L</th>(etc)</tr>";
/* output data of each row */
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["position"]. "</td><td>" . $row["team_name"]. " " . $row["played"]. " " . $row["wins"]. " " . $row["draws"]. (etc)"</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
?>
It should display the table just on its own, but instead it displays all the php code starting at connect_error(. The rest of the site is unaffected.

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.

Mysqli result always returns 0

Hi I can connect to my database but it's says that I have 0 rows!
I've tried different ways to connect to my database, but all of them always return 0 rows so it will not show any data from the database.
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM keys";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Key: " . $row["key"]. " " . $row["used"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
SELECT * FROM keys
^
KEYS is a reserved term, hence your query fails to execute and no data is returned, at the very least escape it.
Best option would be to change your field name to something that's not reserved.
Something funny to note
You're saying if there are some rows in my result show me those but if there are no rows in my result then show me the number of rows. In short, displaying the number of rows when your query fails is utterly pointless! That will be a good place to display an error message returned by the database driver
keys is reserved keyword in mysql it must be in backtick as
$sql = "SELECT * FROM `keys`";
Your query fails and your got 0 result always
To check error in query use
if (!$conn->query("Your_QUERY")) {
printf("Errormessage: %s\n", $conn->error);
}
Read http://php.net/manual/en/mysqli.error.php
I think you have to use this code for get result :-
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM `keys`";
$result = mysqli_query($conn, $sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Key: " . $row["key"]. " " . $row["used"]. "<br>";
}
} else {
echo "0 results";
echo "Rows:". mysqli_num_rows($result);
}
$conn->close();

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

Getting only one row of a mysql table displayed using PHP

I Currently have this code that displays all entries rows from a mysql table and displays them on a web page;
<?php
$servername = "localhost";
$username = "appuser1";
$password = "*****";
$dbname = "acmefg_app";
$row = mysql_fetch_array(mysql_query("SELECT jobnumber FROM appdata WHERE id = '5608' LIMIT 1"));
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, jobnumber, assetnumber FROM appdata";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> id: ". $row["id"]. "<br>";
echo "<br> Job number: ". $row["jobnumber"]. "<br>";
echo "<br> Asset number: " . $row["assetnumber"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
How can I change this so it only displays one result? Lets say in this case I wanted to display the row with an id of 5611? Many Thanks
If you want to show only one row then no need to use while() loop. Remove loop
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
echo "<br> id: ". $row["id"]. "<br>";
echo "<br> Job number: ". $row["jobnumber"]. "<br>";
echo "<br> Asset number: " . $row["assetnumber"] . "<br>";
}
Or can set LIMIT 1 in your query string.
SELECT id, jobnumber, assetnumber FROM appdata LIMIT 1

Categories