could not connect to db [duplicate] - php

This question already has answers here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Why shouldn't I use mysql_* functions in PHP?
(14 answers)
Closed 5 years ago.
Im haveing a problem with this code:
<?php
include 'connect.php';
$SQL = "INSERT into users(username, password, email,)
VALUES ('dado','123','neki#gmail.com',)";
mysql_query($SQL) or die("could not register");
?>
My connect.php looks like this:
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'igra';
?>
When I try to exectue it, Im getting error. Tryed testing connection with
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'igra';
$connect = mysql_connect($dbhost, $dbuser, $dbpass)
or die("Unable to connect to dbhost");
mysql_select_db($dbname)
or die("Could not connect to db");
$test_query = "SHOW TABLES FROM $dbname";
$result = mysql_query($test_query);
$tblCnt = 0;
while($tbl = mysql_fetch_array($result)) {
$tblCnt++;
#echo $tbl[0]."<br />\n";
}
if (!$tblCnt){
echo "There are no tables<br />\n";
} else {
echo "There are $tblCnt tables<br />\n";
}
?>
and it return msg: There are 4 tables. Which means connect.php is properly set. Now I tryed to run code into MySQL Admin program itself with "INSERT into" query, and it worked. So, everything seems to work fine separatly, but when put together, it wont work.
Can somebody point me to misstake Im makeing please?

Related

PHP error: Why isn’t this working, I can't see data result in my page?

I'am setting up a new wampserver and created a new database with a table named 'users'
apache 2.4.23 & PHP 7.1.10 & mySQL 5.7.14
<?php
$server = 'localhost';
$serverUsername = 'root';
$serverPassword = '';
$dbName = 'test';
$connection = mysqli_connect($server,$serverUsername,$serverPassword);
msqli_select_db($dbName);
if(!$connection){
echo 'connection failed to database '.mysqli_connect_error();
}
$sql = "SELECT * FROM users";
$query = mysqli_query($sql);
while($row = mysqli_fetch_array($query)){
print_r($row);
}
?>
my value is really in th data base but nothing appears in the page after running code
Have a look at the comments mentioning the fix
$server = 'localhost';
$serverUsername = 'root';
$serverPassword = '';
$dbName = 'test';
// FIX 1
// You need to mention the database name as the last argument
$connection = mysqli_connect($server,$serverUsername,$serverPassword, $dbName);
if(!$connection){
echo 'connection failed to database '.mysqli_connect_error();
}
$sql = "SELECT * FROM users";
// FIX 2
// The first argument should be your mysqli connection
$query = mysqli_query($connection, $sql);
// Check for errors
if (!$query) {
printf("Error: %s\n", mysqli_error($connection));
exit();
}
while($row = mysqli_fetch_array($query)){
print_r($row);
}
?>
Reference for mysqli_connect: https://secure.php.net/manual/en/function.mysqli-connect.php
Reference for mysqli_query: https://secure.php.net/manual/en/mysqli.query.php

Error while trying to generate JSON Array from MySQL Database in PHP [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
Here is my PHP code for get_categories.php:
<?PHP
require_once('connection.php');
$query="SELECT * FROM categories";
$result = mysqli_query($connection,$query);
$return_arr = array();
while ($row = mysqli_fetch_array($result, mysqli_fetch_assoc)
{
$row_array['category'] = $row['category'];
$row_array['icon'] = $row['icon'];
array_push($return_arr,$row_array);
}
echo json_encode($return_arr);
?>
and connection.php
<?php
$servername = "localhost"; //replace it with your database server name
$username = "root"; //replace it with your database username
$password = "password"; //replace it with your database password
$dbname = "db_client";
// Create connection
$connection = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$connection) {
die("Connection failed: " . mysqli_connect_error());
}
?>
When i run the get_categories.php to generate a json array.. this error appears
Parse error: syntax error, unexpected ';' in C:\xampp\htdocs\get_categories.php on line 7
Can someone correct me on what i am doing wrong? Thanks.
while ($row = mysqli_fetch_array($result, mysqli_fetch_assoc)
should be :
while ($row = mysqli_fetch_array($result, mysqli_fetch_assoc))
You're missing a parenthesis

Get values from mysql database and store in php array [duplicate]

This question already has answers here:
get array of rows with mysqli result
(2 answers)
Closed 5 years ago.
I am working on a project with php and Mysql where I need to get values from Mysql database and store them into array.
Code:
$con=new mysqli("localhost","root","","databse");
$sql="select name from data";
$result= array();
$result=$con->query($sql);
How to store the data in php array?
Here is an example of how to do it!
//MySQLi information
$db_host = "localhost";
$db_username = "username";
$db_password = "password";
//connect to mysqli database (Host/Username/Password)
$connection = mysqli_connect($db_host, $db_username, $db_password) or die("Error " . mysqli_error());
//select MySQLi dabatase table
$db = mysqli_select_db($connection, "table") or die("Error " . mysqli_error());
$sql = mysqli_query($connection, "SELECT * FROM data");
while($row = mysqli_fetch_array($sql)) {
$names[] = $row['name'];
}
Edit:
Print the results out by using
print_r($names);
or
foreach($names as $name) {
echo "$name";
}
Good luck!

How To Get a Single Number From a Database in PHP [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 6 years ago.
I need to echo the number 1 or 0 whatever the database has in that cell. I cant seem to get it to work. What am I doing wrong people of the internet?
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testfp";
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT hookup FROM testfp WHERE name = 100100" ;
$bob = mysql_query($sql);
echo $bob;
?>
You can not use mysql and mysqli together. So you can try the following code (using mysqli only, since mysql is deprecated) :
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testfp";
$conn = new mysqli($servername, $username, $password, $dbname);
//Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT hookup FROM testfp WHERE name = 100100" ;
$result = $conn->query($sql);
if($result->num_rows>0)
{
while($row = $result->fetch_assoc()) {
echo $row["hookup"];
}
}
else
{
echo "No results found!";
}
?>

Getting error: Object of class mysqli_result could not be converted to string [duplicate]

This question already has an answer here:
How to use mysqli_query() in PHP?
(1 answer)
Closed 6 years ago.
My code is very messy, but I am trying to make a forum on my website.
<?php
$conn = mysqli_connect("localhost", "root", "");
if($conn->connect_error){
die("Connection Failed");
}
mysqli_select_db($conn, 'forum');
$get= "select * from forum";
$runq = mysqli_query($conn, $get);
while($fetch_value=mysqli_fetch_array($runq)){
$get_usr_name=$fetch_value['username'];
$get_body = $fetch_value['info'];
}
$result = $conn->query($get);
if(is_null($get_body)){
}
else{
echo "<div id='inputform1'>";
for($row = 1; $row < $result->num_rows; $row++){
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "forum";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
if(mysqli_connect_errno()){
die("database connection failed: ") .
mysqli_connect_errno() .
" (" . mysqli_connect_errno() . ")";
}
$result1 = mysqli_query($connection, "select info from forum where id='$row';");
$result = mysqli_query($connection, "select username from forum where id='$row';");
echo "<strong class=\"after_username\">by: {$result}</strong><br><br><p id=\"bodyforum\">{$result1}<br><br></p>";
}
echo "</div>";
}
?>
I cannot seem to post the username and password; it keeps saying:
Object of class mysqli_result could not be converted to string
I have tried many different "tactics" but none of them seem to work. Can someone help me?
The returned value of a mysqli query is a result object. It's not just a set of rows; it also contains information on the query, success or failure, error messages, and other things. You need to use the result object's methods explicitly to access the rows returned by your query.
I think the problem is from this line
echo "<strong class=\"after_username\">by: {$result}</strong><br><br><p id=\"bodyforum\">{$result1}<br><br></p>";
The $result and $result1 are not strings. You need to loop it up and then access the records.

Categories