Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
My database is on dream host. And its unable to connect .
MY code is
<?php
$hostname = "mysql.demos.smartmobe.com"; // eg. mysql.yourdomain.com (unique)
$username = "nayacinema"; // the username specified when setting-up the database
$password = "****"; // the password specified when setting-up the database
$database = "nayacinema"; // the database name chosen when setting-up the database (unique)
$con=mysqli_connect($hostname,$username,$password,$databse);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}else{
echo 'done';
}
$result = mysqli_query($con,"SELECT * FROM TblUsers");
print_r($result);
while($row = mysqli_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br>";
}
?>
it gives error like this
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/demo_smartmobe/demos.smartmobe.com/nayacinema/test.php on line 20
what may be the problem?
You have a spelling mistake.
$con=mysqli_connect($hostname,$username,$password,$databse);
Should be
$con=mysqli_connect($hostname,$username,$password,$database);
(database spelt wrong)
i have tested your code there was no connectivity.. because of spelling mistake here is correct code , i have tested on my local machine
<?php
$hostname = "localhost"; // eg. mysql.yourdomain.com (unique)
$username = "root"; // the username specified when setting-up the database
$password = ""; // the password specified when setting-up the database
$database = "test"; // the database name chosen when setting-up the database (unique)
$con=mysqli_connect($hostname,$username,$password,$database);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}else{
echo 'done';
}
$result = mysqli_query($con,"SELECT * FROM user");
print_r($result);
while($row = mysqli_fetch_array($result))
{
echo $row['id'] . " " . $row['user_name'];
echo "<br>";
}
?>
Thanks
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I keep on getting this error:
Failed to connect to MySQL: Access denied for user 'root'#'localhost' (using password: XXX)
I am using mamp server to run my web server. why do i get this error? everything looks fine to me but it denys me access.
here is my code:
<?php
$servername = "localhost";
$dbname = "dbtechnerdzzz";
$user = "root";
$password = "";
session_start(); // Starting Session
$error=''; // Variable To Store Error Message
if (isset($_POST['submit'])) {
if (empty($_POST['form-username']) || empty($_POST['form-password'])) {
$error = "Username or Password is invalid";
}
else
{
$connection=mysqli_connect($servername, $user, $password, $dbname);
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// To protect MySQL injection for Security purpose
$username = mysqli_real_escape_string($connection,$_POST['form-username']);
$password = mysqli_real_escape_string($connection,$_POST['form-password']);
// SQL query to fetch information of registerd users and finds user match.
$query = mysqli_query($connection,"select * from accounts where Password='$password' AND Username='$username'");
$rows = mysqli_num_rows($query);
if ($rows == 1) {
$_SESSION['login_user'] = $username; // Initializing Session
header("location: profile.php"); // Redirecting To profile Page
} else {
$error = "Username or Password is invalid";
}
mysqli_close($connection); // Closing Connection
header("location: signin.php"); // Redirecting To login Page
}
}
?>
mamp's Default password is root.
Change your configuration like this.
$servername = "localhost";
$dbname = "dbtechnerdzzz";
$user = "root";
$password = "root";
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
It's possible to create a mysql connection from localhost to another server using php, if it's possible, how? Thanks!
$host_name = "www.yourdomain.com";
$database = "pdo"; // Change your database name
$username = "root"; // Your database user id
$password = "test"; // Your password
try {
$dbo = new PDO('mysql:host='.$host_name.';dbname='.$database, $username, $password);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
Yes it is possible.You must have the username and password of that domain in which you want to connect.
mysqli_connect("www.domain.com","username","password","database_name")or die("Error " . mysqli_error());
Yes, Simply pass following details about your server:
<?php
$servername = "your-server-name-or-ip";
$username = "your-server-username";
$password = "your-server-password";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
$url = 'mysql:host=xxx.xxx.xxx.xxx;dbname=xxxxxx'
$username = xxx;
$password = xxx;
$db = new PDO($url, $username, $password);
$query = $db->query('select * from some_table');
$query->execute();
$res = $query->fetchAll(PDO::FETCH_ASSOC);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I've double-checked and everything looks closed to me, so I can't find the error. I just want to create a table to display mySQL data.
EDIT: I don't know why the closing tag was above the rest of the code, but I still get the error when it's in the correct place.
<?php
$servername = "localhost";
$username = “x”;
$password = “x”;
$dbname = “x”;
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM Classroom”;
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
echo “<tr><th>Building</th><th>Floor</th><th>Room</th><th>Instructional</th><th>Type<th>Size</th>
<th>Seating</th><th>Decking</th><th>Access</th><th>Whiteboard</th><th>Chalkboard</th></tr>”;
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo “<tr><td>”.$row[“building”].”</td></tr>”;
}
} else {
echo (“0 results”);
}
mysqli_close($conn);
}
?>
Edit: As per your original post https://stackoverflow.com/revisions/27974352/1
This should go at the very bottom:
?>
In fact, it isn't even required unless you're going to put some pure HTML after it. So leaving it out completely might save you headaches in the future.
However, several of your double-quotes look funky pasted here. You might check that they are just double-quotes, and not special characters.
These curly/smart quotes “ ” should be replaced by regular double quotes " throughout your code.
Those alone will break its functionality and cause a parse/syntax error.
Edit: As per your edit: You need to remove the last } in your file, the one just after mysqli_close($conn);. The number of braces do not match.
This works!
<?php
mb_internal_encoding('UTF-8');
$servername = "localhost";
$username = "x";
$password = "x";
$dbname = "x";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM Classroom";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
echo "<tr><th>Building</th><th>Floor</th><th>Room</th><th>Instructional</th><th>Type<th>Size</th>
<th>Seating</th><th>Decking</th><th>Access</th><th>Whiteboard</th><th>Chalkboard</th></tr>";
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<tr><td>".$row["building"]."</td></tr>";
}
}else{
echo("0 results");
}
mysqli_close($conn);
?>
Remove ?> from all your documents, as it's unneeded, as PHP self closes at the end of a file.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
How do i write the connection string to connect to mysql database with this info:
Using php connection string
Database Details:
phpmyadmin url: https://medicalng.com/phpmyadmin
host: localhost
database: wlfmedic_ptest1
username: wlfmedic_ptest1
password: Prog#te$t104
Use mysqli. mysqli connection is like this:
$host = "localhost";
$database = "wlfmedic_ptest1";
$username = "wlfmedic_ptest1";
$password = 'Prog#te$t104';
$connection = mysqli_connect($host, $username, $password, $database);
if(mysqli_connect_errno()){ //To show error if fails to connect
die(mysqli_connect_error());
}
Something basic, not secure tho.
<?php
mysql_connect(localhost, ##YOUR USERNAME##, ##YOUR PASSWORD##);
mysql_select_db('##YOUR DATABASE NAME##');
$result = mysql_query("SELECT * FROM emails");
?>
and updated mysqli http://www.sanwebe.com/2013/03/basic-php-mysqli-usage
<?php
$db_hostName="localhost";
$databaseName= "wlfmedic_ptest1";
$db_userName= "wlfmedic_ptest1";
$db_password= "Prog#te$t104";
$con=mysql_connect($db_hostName,$db_userName,$db_password);
if(!$con){
die ("Could Not successfully with DataBase".mysql_error());
}else{
echo "Connect connect with succssfully .";
}
$db=mysql_select_db($db_databaseName,$con);
if(!$db)
{
echo "Can notConnect with Bd Social";
}
else
{
echo "Conncet With Bd Social";
}
?>
try this ..
<?PHP
$user_name = "root";
$password = "";
$database = "addressbook";
$server = "127.0.0.1";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
print "Database Found ";
mysql_close($db_handle);
}
else {
print "Database NOT Found ";
}
?>
also you can refer this link
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Any one knows why my $result echo in footer instead of body? Is it an html maybe div problem or PHP?
Here is a something similar to what I have:
<?php
// 1. Create a database connection
$dbhost = "localhost";
$dbuser = "tester";
$dbpass = "12345";
$dbname = "practice";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Test if connection occurred.
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
?>
<?php
// 2. Perform database query
$SQLstring = "SELECT *FROM user ORDER BY username";
// Test if there was a query error
$QueryResult = #mysqli_query($connection, $SQLstring)
Or die("<p>Unable to execute query.</p>"
. "<p>Error code " . mysqli_errno($connection)
. ": " . mysqli_error($connection)) . "</p>";
?>
<!DOCTYPE>
<html>
<head>
</head>
<body>
<?php
//3. uses the mysqli_assoc() to print table
echo "<table>";
echo "<tr><th>Name</th><th>Address</th><th bgcolor='#0099FF'
align='left'>City</th><th bgcolor='#0099FF' align='left'>State</th></tr>";
$num_results = $QueryResult->num_rows;
for ($i=0; $i <$num_results; $i++) {
// 3. Use returned data (if any)
$Row = mysqli_fetch_assoc($QueryResult); //associative array
// output data from each row
echo "<tr><td>{$Row['username']}</td>";
echo "<td>{$Row['address']}</td>";
echo "<td>{$Row['city']}</td>";
echo "<td>{$Row['state']}</td>";
</tr>";
}
// 4. Release returned data - close query
mysqli_free_result($QueryResult);
?>
</body>
</html>
<?php
// 5. Close database connection
mysqli_close($connection);
?>
Any ideas? The table prints out fine no problem just not in the correct place on the site
echo "<td>{$Row['city']}</td>";
echo "<td>{$Row['state']}</td>";
</tr>"; //You got an error here!
}
I think you forgot to echo this line.
you should get parse error but i could not point that
just try change this lines
// output data from each row
echo "<tr><td>{$Row['username']}</td>";
echo "<td>{$Row['address']}</td>";
echo "<td>{$Row['city']}</td>";
echo "<td>{$Row['state']}</td>";
echo "</tr>";
}