I'm relatively new to any type of programming or coding. I'm not quite understanding why no matter what adjustments I make to my php file I can't seem to pull any data from a table.
Here is a link to the table: https://i.gyazo.com/4ad5e860895014c49dbe0539c38cdec2.png
Above is the test table I have been trying to use. From what I can understand I'm connecting to the database okay, but all of my problems come after the connection. Also, I'm using php 7.0 so a lot of the information I'm finding online has not been helpful.
If there is something glaringly wrong with my table or in my code, please let me know.
Here is my code:
'''
//Set Variables
$serverName = "localhost";
$userName = "root";
$password = "";
$databaseName = "test";
//Create Connection
$connection = mysqli_connect($serverName,$userName,$password,$databaseName);
//Check Connection
if(!$connection){
die("Connection failed: ".mysqli_connect_error());
}
echo "Connected successfully <br>";
//Fetch Data
$query = "SELECT * from table1";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_array($result, MYSQLI_NUM);
printf ($row[1], $row[2]);
mysqli_free_result($result);
mysqli_close($connection);
I figured out the issue a few hours ago. The code I had posted would have worked perfectly fine if I was connecting to the port for MySQL rather than MariaDB. Didn't realize that the port that MariaDB was connected to was the default.
MariaDB by default was port 3306, but MySQL was 3308. After specifying 'localhost:3308' I was able to start properly pulling rows from my tables.
Related
This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 4 years ago.
Working with a Wordpress website that is being hosted by Godaddy. I am familiar with creating custom templates and scripting pages (php) and running them on this specific Wordpress webpage.
I am at the point where I need to access my mysql database and interact with custom templates+data via programmatically. Just in the past few days I've searched hundreds threads+tutorials and copied examples with no luck. I have a created a php file called display_data.php
Code:
<?php /* Template Name: display_data */ ?>
<?php
$db_host = 'localhost'; // Server Name
$db_user = 'root'; // Username
$db_pass = ''; // Password
$db_name = 'tutorial'; // Database Name
$conn = mysql_connect($db_host, $db_user, $db_pass, $db_name);
if (!$conn) {
die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}
$sql = 'SELECT *
FROM sales';
$query = mysqli_query($conn, $sql);
if (!$query) {
die ('SQL Error: ' . mysqli_error($conn));
}
?>
I get this error:
"Failed to connect to MySQL: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)"
I have had no luck figuring out what this error specifically means in my case and I've searched for many hours.
I am at the point where I believe I am missing something stupid simple that I am looking past.
Either I am filling the parameters wrong - $db_user, $db_pass, $db_name, or missing something entirely.
I used the same parameters (User name, password) from the ones I got from Godaddy to access phpmyadmin, and for $db_name I used the one I see in my database (picture).Taken from my database, I used what is said to the right of "Database:"
Taken from Gogdaddy (Database PHPMyadmin - View)
Please any help is greatly appreciated, and be nice. I've never used mysql before but I am also not a total noob to programming.
Thank you.
Why dont you just try using wpdb? This is what you should use to work with secondary databases in WP.
$mydb = new wpdb('username','password','database','localhost');
$rows = $mydb->get_results("select Name from my_table");
echo "<ul>";
foreach ($rows as $obj) :
echo "<li>".$obj->Name."</li>";
endforeach;
echo "</ul>";
I'm trying to figure out how can I put two differents MYSQL database connections name on the same page, one is for local PC and one is for hosting server.
Does is possible to have two different databases servers name on the same page so that way not to change connection database name in a script before upload.
I have code like this for local PC
<?php
$username = "your_name";
$password = "your_password";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
?>
Can can i add another connection name in the server in same page without changing the connection database!
alidad
Yes, a single page may query as many different servers as needed:
<?php
$server1 = new mysqli("server1.example.com", "user1", "password1", "database1");
$server2 = new mysqli("server2.example.com", "user2", "password2", "database2");
$result = $server1->query("SELECT 'Hello user' AS _message FROM DUAL");
$row = $result->fetch_assoc();
echo htmlentities($row['_message']);
$result = $server2->query("SELECT 'Hello user' AS _message FROM DUAL");
$row = $result->fetch_assoc();
echo htmlentities($row['_message']);
The way is to have a config.ini file in your localhost, and a different one in your production environment. This way you will parse this file and get your credentials.
A better way to do that is using this library: https://github.com/vlucas/phpdotenv
It works basically on the same way, but is easy to maintain.
I am trying to connect to a simple database on XAMPP using php- I know the database exists as I can see it on PHPMyAdmin and have created a table called students and added some data.
I have tested that I can run a simple test.php file ( from the htdocs folder on the XAMPP drive) and get a response. I cannot spot what is stopping me connecting to my database- can anyone help?
<?php
// connect to the database
$user_name = "root";
$password = "";
$database = "computing";
$host_name ="localhost";
$con=mysql_connect($host_name,$user_name,$password);
mysql_select_db($database);
//check connection
echo "Connection opened";
mysql_close($con);
?>
Could you please try the following code if it works?
<?php
// connect to the database
$user_name = "root";
$password = "";
$database = "computing";
$host_name = "localhost";
$con = mysqli_connect($host_name ,$user_name ,$password,$database) or die("Error " . mysqli_error($con));
//check connection
echo "Connection opened";
mysql_close($con);
?>
mysql commands are not going to be supported in future releases, so it would be best perhaps to use mysqli or PDO connections.
Also PDO uses parameters (the syntax might take a bit to make sense), so it is great to reduce risk from SQL Injections.
Mysqli: http://php.net/manual/en/function.mysqli-connect.php
PDO: http://php.net/manual/en/class.pdo.php
The code above should work. Maybe try mysql_select_db($database, $con);
For some reason, the following code inside the query works in my MySQL command console, yet when I try to run it as a Query in PHP, something keeps going wrong and I'm not sure what. Here is the code I've done so far.
//2. Perform database query
$query = "SELECT skills.element_id, content_model_reference.element_id, element_name FROM skills, content_model_reference WHERE (skills.element_id = content_model_reference.element_id)";
$result = mysql_query($query);
//Tests if there was a query error
if(!$result){
die("Database query failed.");
}
Is there something preventing the code that worked in MySQL (The line with SELECT) from working, or is my syntax somehow wrong?
EDIT: So it's saying I didn't select a database. Yet I thought I had. Here is the code above it:
//1. Create a database connection
$dbhost = "host"; //Host: Can be either an IP address, or a domain (like google.com).
$dbuser = "user";//User: The user that is connecting to the database.
$dbpass = "pass";//Password: This is the password that the user is using.
$dbname = "db";//Name: This is the name of the database.
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);//The value, 'handle,' is the connection.
//Test if connection occurred. Die ends the program/php, and in this case, also prints a message
if(mysqli_connect_errno()){
die("Database connection failed: ".
mysqli_connect_error().
" (". mysqli_connect_errno() . ")"
);
}
Like I said, the error message I am getting is pertaining only to the query, the server is fine with my database connection.
You're using mysqli_* for the connection, but you're using mysql_* for the QUERY... don't think you can do that, has to be one or the other (MYSQLI_ preffered). Also the query should be:
$result = mysqli_query($connection,$query);
I am a complete database newbie. So far, I know that I can connect to MySQL using PHP's mysql_connect() command, but apart from that, I really don't see how to take that data and put it onto a web page.
a) are there ways other than mysql_connect()
b) lets say I had a table of data in mysql and all I wanted was for that table (for example: list of names and telephone numbers) to now appear on my web page. I can't for the life of me find a tutorial for this.
<?
$database_name = "dbname";
$mysql_host = "localhost"; //almost always 'localhost'
$database_user = "dbuser";
$database_pwd = "dbpass";
$dbc = mysql_connect($mysql_host, $database_user, $database_pwd);
if(!$dbc)
{
die("We are currently experiencing very heavy traffic to our site, please be patient and try again shortly.");
}
$db = mysql_select_db($database_name);
if(!$db)
{
die("Failed to connect to database - check your database name.");
}
$sql = "SELECT * FROM `table` WHERE `field`= 'value'";
$res = mysql_query($sql);
while($row = mysql_fetch_assoc($res)) {
// code here
// access variables like the following:
echo $row['field'].'<br />';
echo $row['field2'];
}
?>
Check out mysql_fetch_assoc mysql_fetch_array and mysql_fetch_object
This is the very basics, you will want to search for tutorials. There are many about.