How to Connect two data base mysql in same time - php

I use this in a file called connect.php:
$username_mysql = "root";
$password_mysql = "";
$database_mysql = "bdd_imvert";
//$database_mysql = "imprimvert_renouvellement";
$db = #mysql_connect($hostname_mysql, $username_mysql, $password_mysql) or die(mysql_error());
mysql_select_db($database_mysql,$db);
$hostnameivert_mysql = "localhost";
$usernameivert_mysql = "root";
$passwordivert_mysql = "";
$databaseivert_mysql = "ivert";
//$database_mysql = "imprimvert_renouvellement";
$db1 = #mysql_connect($hostnameivert_mysql, $usernameivert_mysql, $passwordivert_mysql) or die(mysql_error());
mysql_select_db($databaseivert_mysql,$db1);`
I include it in the a page containing:
$sql = "select * from ivert_users where id=".GetSQLValueString($user_id,"int");
$re = #mysql_query($sql,$db)
I get an error when I do this however:
'ivert.ivert_users' does not exist
I am not sure why I get this error, any help would be appreciated.

There is no problem with your code. I tried the same and it is working for me. There must be some problem with your query. Because your error message shows that connection happened but your query returned an error. Refer this It might help.
Or the second reason it can be is you haven't defined host for the first DB.
<?php
$hostnameivert_mysql = "localhost";
$username_mysql = "root";
$password_mysql = "pma";
$database_mysql = "olivenation";
$db = #mysql_connect($hostname_mysql, $username_mysql, $password_mysql) or die(mysql_error());
mysql_select_db($database_mysql,$db);
echo ("<pre>");print_r($db);echo("</pre>");
$hostnameivert_mysql = "localhost";
$usernameivert_mysql = "root";
$passwordivert_mysql = "pma";
$databaseivert_mysql = "practical";
$db1=#mysql_connect($hostnameivert_mysql, $usernameivert_mysql, $passwordivert_mysql) or die(mysql_error());
mysql_select_db($databaseivert_mysql,$db1);
echo ("<pre>");print_r($db1);echo("</pre>");
$sql = "select * from product_master where id=1";
$re = #mysql_query($sql,$db);
echo ("<pre>");print_r(mysql_fetch_array($re));echo("</pre>");
$sql = "select * from users";
$re = #mysql_query($sql,$db1);
echo ("<pre>");print_r(mysql_fetch_array($re));echo("</pre>");
?>

Related

MySQL in PHP - WHERE clause - not working

I have this code:
<?php
$user = $_COOKIE["user"];
$password = $_COOKIE["password"];
$localhost = "localhost";
$userdb = "xxxxx";
$passworddb = "xxxxx";
$database = "xxxxx";
$conn = mysqli_connect($localhost, $userdb, $passworddb, $database);
$vyber = "SELECT PASSWORD FROM Login WHERE User=".$user;
$result = mysqli_query($conn, $vyber);
echo $result;
?>
Cookie are set and if I use $vyber in database so everything is good. But there PHP write nothing. Can anybody tell, what I doing wrong? (Without comand $vyber every thing running perfect)
instead of,
echo $result
try to do that :
while ($row = mysqli_fetch_row($result)){
echo $row[0];
}
It is query error change query to :
$vyber = "SELECT PASSWORD FROM Login WHERE User='$user'";
if did not work use die function to dispaly error message :
mysqli_query($conn, $vyber) or die(mysqli_error($conn));
To fetch records :
while ($row = mysqli_fetch_array($result)){
echo $row[0];
}

how to paginate data fetched from databse

lets say if there are 13 records the latest 5 or from 9-13 in the first page,
from 4-8 in second page and 1-3 in the third page
i've tried this but its for the first page only
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mysqli_login";
// Create connection
$connection= new mysqli($servername, $username, $password, $dbname);
$query = mysqli_query($connection, "SELECT name,submittedby,trn_date FROM new_record ORDER BY id DESC LIMIT 5")or die(mysqli_error($connection));
while ($row = mysqli_fetch_array($query)) {
$fileName = $row['name'];
$fileContents = file_get_contents("txt/$fileName");
$poster = $row['submittedby'];
$date = $row['trn_date'];
echo ("posted by :$poster | posted date : $date");
echo ("$fileContents");
}
?>
Your code should be like this
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test_db";
// Create connection
$connection= new mysqli($servername, $username, $password, $dbname);
$current_page = 1; // 1=>refer to the first page, 2=> second page and so on..
if(!empty($_GET['page_no']){
$current_page = $_GET['page_no'];
}
$to = "5"; // it is no of record which you wants to show on each page, you can change it's value as per your need.
$from = ($current_page - 1) * $to;
$query = mysqli_query($connection, "SELECT name,submittedby,trn_date FROM new_record ORDER BY id DESC LIMIT $from , $to")or die(mysqli_error($connection));
while ($row = mysqli_fetch_array($query)) {
$fileName = $row['name'];
$fileContents = file_get_contents("txt/$fileName");
$poster = $row['submittedby'];
$date = $row['trn_date'];
echo ("posted by :$poster | posted date : $date");
echo ("$fileContents");
}
?>
your url should be something like http://localhost/projects/?page_no=1
replace "http://localhost/projects/" with your actual url
Hope this will help!!
Here's an article that discusses this topic. You need to offset your query based on the current page. So it will be LIMIT 5 OFFSET <amount based on the page>.

mysql can't return a row result

I am connected to the database, a page has lots of content so I'll only share the part that doesnt return a value in php, but it returns a value in MySQL
Here is the code;
$query = "SELECT firstname FROM users WHERE id = '17'";
$query_run = mysql_query($query);
$row = mysql_fetch_row($query_run);
echo $row[0];
Changing the code I shared first to this, solved the problem. Thanks to anyone who tried to help.
$query = "SELECT firstname FROM users WHERE id = '17'";
$query_run = mysqli_query($conn, $query);
$row = mysqli_fetch_row($query_run);
echo $row[0];
And made bit changes to the connect.inc.php which I also shared in comment.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "notsitesi";
$conn = new mysqli($servername, $username, $password, $dbname);
if($conn->connect_error) {
die("Connection failed");
}
?>

Updating mysql database through php with HTTP POST methods

It executes on application perfectly I double checked it. It sends all parameters properly and the problem is in php script it doesn't execute the query (update,delete) it execute the insert query properly. The php script works perfect with html form but I don't know where the problem is.
Here is my php script:
$mysql_host = "localhost";
$mysql_database = "locator";
$mysql_user = "root";
$mysql_password = "";
mysql_connect($mysql_host,$mysql_user,$mysql_password) or die(mysql_error());
mysql_select_db($mysql_database);
$uMail = $_POST['u_mail'];
$uIMEI = $_POST['u_IMEI'];
$uName = $_POST['u_name'];
$uPass = $_POST['u_pass'];
$tName = $_POST['t_names'];
$tIMEI = $_POST['t_IMEIs'];
$tDesc = $_POST['t_desc'];
mysql_query("UPDATE user_master SET t_names='$tName',t_IMEIs='$tIMEI',t_desc= '$tDesc' WHERE u_mail = '$uMail' AND u_IMEI = '$uIMEI'");
mysql_close();
?>
maybe you should escape your strings with mysql_real_escape_string()
$uMail = mysql_real_escape_string($_POST['u_mail']);
$uIMEI = mysql_real_escape_string($_POST['u_IMEI']);
$uName = mysql_real_escape_string($_POST['u_name']);
$uPass = mysql_real_escape_string($_POST['u_pass']);
$tName = mysql_real_escape_string($_POST['t_names']);
$tIMEI = mysql_real_escape_string($_POST['t_IMEIs']);
$tDesc = mysql_real_escape_string($_POST['t_desc']);
mysql_query("UPDATE user_master SET t_names='$tName',t_IMEIs='$tIMEI',t_desc= '$tDesc' WHERE u_mail = '$uMail' AND u_IMEI = '$uIMEI'");
and make sure $uMail and $uIMEI are set correctly

Comparing database stringvalue with new stringvalue

Here is what I'm trying to do: When user adds a contact to his list, the number of this contact gets run by with the numbers in the database and it gives feedback if the user is already in the database or not. Right now I always get back "User is in database" even though he isn't. Then again I'm not that well acquainted with php. I changed the code a bit again, now it doesn't work at all, because it doesn't like the part
$number = ($_GET["number"] from $DB_Table);
Full code
<?php
$DB_HostName = "localhost";
$DB_Name = "db";
$DB_User = "user";
$DB_Pass = "pw";
$DB_Table = "contacts";
$number = ($_GET["number"] from $DB_Table);
$fnumber = ($_GET["fnumber"]);
if ($number == $fnumber) {
echo "This user is already in database";
} else {
echo "This user isn't in the database";
}
$con = mysql_connect($DB_HostName,$DB_User,$DB_Pass) or die (mysql_error());
mysql_select_db($DB_Name,$con) or die(mysql_error());
mysql_close($con);
?>
I don't actually see you executing the database query. You could do something like this:
<?php
$DB_HostName = "localhost";
$DB_Name = "db";
$DB_User = "user";
$DB_Pass = "pw";
$DB_Table = "contacts";
$con = mysql_connect($DB_HostName,$DB_User,$DB_Pass) or die (mysql_error());
mysql_select_db($DB_Name,$con) or die(mysql_error());
$fnumber = mysql_real_escape_string($_GET["fnumber"]);
$result = mysql_query("SELECT * FROM $DB_Table WHERE Something = '$fnumber'", $con);
if ($result) {
// Check the number of rows in the result set
if (mysql_num_rows($result) > 0) {
echo "This user is already in database";
}
else echo "This user isn't in the database";
}
mysql_close($con);
?>
This is not valid PHP code: $number = ($_GET["number"] from $DB_Table);
$_GET["number"] represents the value of the "number" parameter that you find in the url of your page.
Example: http://example.com/index.php?number=7 so $_GET["number"] is 7.
In your code, $DB_Table is a just a string ("contact") and "from" does not fit there using php syntax.
mysql_select_db($DB_Name,$con) or die(mysql_error());
is valid PHP but you are not doing anything with what you get from the database. I suggest you at least take a look at this tutorial php mysql select

Categories