I have a simple studentinfo.accdb. I was able to connect to this database using php. Moreover, the values of the 'ID' column(primary key) are also being displayed. however, the field values are not. The error message is:
Connected
ID : 1
Warning: odbc_result(): Field class not found in
C:\xampp\htdocs\connect\index.php on line 20
The following is my code:-
<?php
$con=odbc_connect("studentinfo", "", "");
if($con)
{
echo "Connected<br>";
}
else
{
echo "Failed";
}
$sql="select * from Table1 WHERE ID = 1";
$result=odbc_exec($con, $sql);
while ($row=odbc_fetch_array($result)) {
echo "ID : ". $row['ID'];
if(isset($_GET['tName'])){
echo "NAME : ". $row['tName'];}
$asd = odbc_result($result, "class");
echo $asd;
// echo "CLASS :".$row['class'];
echo "<br/>";
}
?>
I have used an isset() and clearly the fileds are not set for some reason. Please help!
Related
Hi im trying to delete a users booking detials when the user clicks delete in my bookingbeforedeltion.php file but for some reason when I test my php file once I click delete it goes to my delete.php screen and says it failed to delete from database and has the error Undefined index: rn. Is my rn not defined? Sorry Im new to this. Here is my code below:
bookingbeforedeltion.php:
<!DOCTYPE HTML>
<html><head><title>BookingBeforeDeletion</title> </head>
<body>
<?php
include "config.php";
$DBC = mysqli_connect("127.0.0.1", DBUSER , DBPASSWORD, DBDATABASE);
if (!$DBC) {
echo "Error: Unable to connect to MySQL.\n".
mysqli_connect_errno()."=".mysqli_connect_error() ;
exit;
};
echo "<pre>";
$query = 'SELECT roomname, checkindate, checkoutdate FROM booking';
$result = mysqli_query($DBC,$query);
if (mysqli_num_rows($result) > 0) {
echo "Delete Bookings" ?><p><?php
while ($row = mysqli_fetch_assoc($result)) {
echo "Room name: ".$row['roomname'] . PHP_EOL;
echo "Check in date: ".$row['checkindate'] . PHP_EOL;
echo "Check out date: ".$row['checkoutdate'] . PHP_EOL;
?>
[Cancel]
<?php
echo "<hr />";
}
mysqli_free_result($result);
}
echo "</pre>";
echo "Connectted via ".mysqli_get_host_info($DBC);
mysqli_close($DBC);
?>
</body>
</html>
delete.php:
<!DOCTYPE HTML>
<html><head><title>BookingBeforeDeletion</title> </head>
body>
<?php
include "config.php";
$DBC = mysqli_connect("127.0.0.1", DBUSER , DBPASSWORD, DBDATABASE);
if (!$DBC) {
echo "Error: Unable to connect to MySQL.\n".
mysqli_connect_errno()."=".mysqli_connect_error() ;
exit;
};
echo "<pre>";
$roomname=$_GET['rn'];
$query = "DELETE bookingID, roomname, checkindate, checkoutdate, contactnumber,
bookingextras, roomreview, customerID, roomID FROM booking WHERE roomname =
'$roomname'";
$result = mysqli_query($DBC,$query);
if($result)
{
echo "<font color='green'> Booking deleted from database";
}
else {
echo "<font color='red'> Failed to delete booking from database";
}
?>
and I think this will help:
As mentioned above, you need to print it from the PHP
<a href= 'delete.php?rn=$result[roomname]'>
// To
<a href= 'delete.php?rn=<?= $row['roomname'] ?>'>
// Explanation:
// 1. <?= ... ?> is the short form of <?php echo ... ?>
// 2. The `roomname` came from $row, not $result ($result is the MySQLi Object)
// 3. You need to quote the `roomname` because without it `roomname` will be readed
// as Constant, and may Throw a Warning message
//
Your DELETE is incorrect, the correct one is DELETE FROM ... WHERE ...
$query = "DELETE bookingID, roomname, checkindate, checkoutdate, contactnumber,
bookingextras, roomreview, customerID, roomID FROM booking WHERE roomname =
'$roomname'";
// To
$query = "DELETE FROM booking WHERE roomname = '$roomname'";
EXTRA:
3. You can assign a default value to $roomname
$roomname=$_GET['rn'];
// To
$roomname=$_GET['rn'] ?? 'default if null';
// if the rn index doesnt exist, the $roomname value will be `default if null` instead of throwing a Warning
Try to use Prepared-Statement SQL instead of writing it. (I dont know the example, but it can prevent SQL Injection)
I wan to insert data to mysql table from another database which is connected via ODBC.But I cannot enter into the while loop, here is my code -
N.B: For security I dont provide db name, user and pass.
ODBC connection declared as 'connStr'
<?php
$connStr = odbc_connect("database","user","pass");
$conn = mysqli_connect("server","user","pass","database");
//$result_set=mysqli_query($conn,$datequery);
//$row=mysqli_fetch_array($result_set);
echo "<br>";
echo "<br>";
$query="select cardnumber, peoplename, creditlimit, ROUND(cbalance,2) as cbalance, minpay from IVR_CardMember_Info
where cardnumber not like '5127%'" ;
$rs=odbc_exec($connStr,$query);
$i = 1;
while(odbc_fetch_row($rs))
{ //echo "Test while";
$cardnumber=odbc_result($rs, "cardnumber");
$peoplename=odbc_result($rs, "peoplename");
$creditlimit=odbc_result($rs, "creditlimit");
$cbalance=odbc_result($rs, "cbalance");
$minpay=odbc_result($rs, "minpay");
$conn = mysqli_connect("server","user","pass","database");
$sql= "INSERT INTO test_data(cardnumber, peoplename, creditlimit, cbalance, minpay) VALUES ('cardnumber', 'peoplename', 'creditlimit', 'cbalance', 'minpay') ";
if(!(mysqli_query($conn,$sql))){
//echo "Data Not Found";
echo "<br>";
}
else{
echo "Data Inserted";
echo "<br>";
}
echo $i++ ;
}
echo "<br>";
odbc_close($connStr);
?>
How can I solve this?
If you really want to understand why you can't enter while() {...}, you need to consider the following.
First, your call to odbc_connect(), which expects database source name, username and password for first, second and third parameter. It should be something like this (DSN-less connection):
<?php
...
$connStr = odbc_connect("Driver={MySQL ODBC 8.0 Driver};Server=server;Database=database;", "user", "pass");
if (!$connStr) {
echo 'Connection error';
exit;
}
...
?>
Second, check for errors after odbc_exec():
<?php
...
$rs = odbc_exec($connStr, $query);
if (!$rs) {
echo 'Exec error';
exit;
}
...
?>
you can do it with just SQL check it here, like:
INSERT INTO test_data(cardnumber, peoplename, creditlimit, cbalance, minpay)
SELECT cardnumber, peoplename, creditlimit,
ROUND(cbalance,2) as cbalance, minpay from IVR_CardMember_Info
where cardnumber not like '5127%'
I have a problem with getting a value from 'last_insert_id()'..
I want to insert a same id number to id of table 'memo'. However, the problem is after foreach, last_insert_id() gets a new value, so, I tried to make a variable and get a value from 'last_insert_id()', but it didn't work..
$diary_id = 'last_insert_id()';
foreach ($_POST['memo'] as $memo) {
echo "You selected: $diary_id <br>";
echo "You selected: $memo <br>";
$query_test = "insert into memo(memo_no,id,memo)
values(NULL,$diary_id,'$memo')";
mysql_query($query_test, $connect);
}
i am assuming $connect is a database connection link so use mysql_insert_id($connect) then try :
<?php
$diary_id = mysql_insert_id($connect);
foreach ($_POST['memo'] as $memo) {
echo "You selected: $diary_id <br>";
echo "You selected: $memo <br>";
$query_test = "insert into memo(memo_no,id,memo) values(NULL,$diary_id,'$memo')";
mysql_query($query_test, $connect);
}
?>
Use mysql_insert_id function in php to get the last previous AUTO INCREMENTED generated id. Use the code below
$diary_id = mysql_insert_id();
foreach ($_POST['memo'] as $memo) {
echo "You selected: $diary_id <br>";
echo "You selected: $memo <br>";
$query_test = "insert into memo(memo_no,id,memo)
values(NULL,$diary_id,'$memo')";
mysql_query($query_test, $connect);
}
Hope this helps you
Hi I am trying to make status of user so it will check how many post this user has and echo out the result, it was working fine when I was using mysql but after converting it to mysqli it giving me some errors
Here is my code:
<?php
if(!isset($_COOKIE['loggedin'])){
header("location:index.php");
}
session_start();
if(!isset($_SESSION['user'])){
header("location: index.php");
}
else {
?>
<?php
$con=mysqli_connect("localhost","root","123","user");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$resule = "SELECT count(ID) from save_data where username = '".$_SESSION['user']."'"
or die(mysql_error());
$result = $con->query($resule);
$row = $result->fetch_array(MYSQLI_NUM);
echo $row[0], $row[1];
?>
<?php }?>
Error:
Notice: Undefined offset: 1 in C:\xampp\htdocs\mysql_login\status.php on line 30
Try removing $row[1] from:
echo $row[0], $row[1];
As the error says the offset 1 is undefined. so its pretty simple that you should remove $row[1] from echo $row[0], $row[1];
The problem is $row[1] in echo $row[0], $row[1]; because the $result->fetch_array will only fill the $row[0], because the SQL Query
"SELECT count(ID) from save_data where username =
'".$_SESSION['user']."'"
will only return one result if username is unique.
What will be happened If your query doesn't return any values? So better you check
if($resesult)
{
$row = $result->fetch_array(MYSQLI_NUM);
echo $row[0];// remove $row[1] because it fills only the $row[0]
}
I use this code to select password field from the sql server 2012 db, and does not return any data
I tried to change the field type and it did work
actually I can't change the field type in the main server and I need to work with it as it is
any idea how to work around it?
<?php
$objConnect = mssql_connect("localhost:1434\MSSQLSERVER","fdi","fdifdi");
if($objConnect)
{
echo "Database Connected.<br />";
mssql_select_db('Intranett', $objConnect);
$query = mssql_query('SELECT [pass] FROM [Intranett].[dbo].[v24Brukere]');
// Check if there were any records
if (!mssql_num_rows($query)) {
echo 'No records found';
} else {
// Print a nice list of users in the format of:
// * name (username)
echo '<ul>';
while ($row = mssql_fetch_object($query)) {
echo '<li>' . $row->pass .' </li>';
}
echo '</ul>';
}
}
else
{
echo "Database Connect Failed.<br />";
echo mssql_get_last_message();
}
mssql_close($objConnect);
?>
As far as I know, NVARCHAR is not supported with the old and long outdated mssql-drivers. You should instead use Microsoft's SQL Server Driver for PHP.